我有一个节点控制台应用程序,在该应用程序中,我试图向本地CouchDB实例上的批量文档api发送大量记录,但得到的结果是"Invalid UTF-8JSON“。特别奇怪的是,我在对象文字上使用JSON.stringify生成我的JSON。
我在http://jsonlint.com/测试了实际的json,假设它是有效的。我不能在这里发布完整的json,因为它的名称和编号来自目录,但我可以向您展示一个模拟记录,以给您基本的结构。
{
"family" : "Smith",
"people":{
"Bob":{
"name":"Bob",
"active" : true,
"birthday" : "1/01"
},
"Sue":{
"name": "Sue",
"active" : true,
"birthday" : "1/01"
}
},
"address": {
"street" :"1111 Cool Road",
"city" : "Cincinnati",
"state" : "OH",
"zip" : "11111"
},
"phone" : "923-4908"
};
我将我的记录数组包装在一个json对象中,该对象具有指定的here属性"docs“。我正在尝试做的事情有什么明显的错误吗?
更新:根据lambmj的建议,我将node生成的字符串写到一个文件中,然后使用curl将其发送到批量文档上传。这样做效果很好。我仍然希望有人能帮我纠正我的节点代码,这样它就可以在节点内部工作。下面是构建和发布请求的确切代码
//The result variable is my array of 'family' objects,
//It's generated by parsing a text file, not show here
var postOptions = {
host: 'localhost',
port: '5984',
path: '/members/_bulk_docs',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': result.length
}
};
var request = http.request(postOptions, function(res){
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ' + chunk);
});
});
var body = { "docs": result};
var stringData = JSON.stringify(body);
console.log(stringData);
request.write(stringData);
发布于 2012-11-01 23:00:23
我能够使用以下命令让您的示例正常工作:
curl -X POST -H "Content-type:application/json" \
http://localhost:5984/test/_bulk_docs -d @family.json
其中,family.json
包含以下内容。我添加了第二个族,以便在数组中有多个元素。
{"docs": [
{"family" : "Smith",
"people":{
"Bob":{
"name":"Bob",
"active" : true,
"birthday" : "1/01"
},
"Sue":{
"name": "Sue",
"active" : true,
"birthday" : "1/01"
}
},
"address": {
"street" :"1111 Cool Road",
"city" : "Cincinnati",
"state" : "OH",
"zip" : "11111"
},
"phone" : "923-4908"
},
{"family" : "Jones",
"people":{
"John":{
"name":"John",
"active" : true,
"birthday" : "1/01"
},
"Mary":{
"name": "Mary",
"active" : true,
"birthday" : "1/01"
}
},
"address": {
"street" :"1112 Cool Road",
"city" : "Cincinnati",
"state" : "OH",
"zip" : "11111"
},
"phone" : "923-4909"
}
]}
JSON与数组中提供的JSON相同,并作为传递给CouchDB的JSON的docs
属性值提供。在您给出的示例中还有一个尾随的分号(;
)。如果其他内容的格式都正确,这可能就是问题所在。
更新:
好的,这是答案节点代码。按照代码中的说明,我做了3处更改:
#!/usr/bin/env node
var http = require("http");
// Embedding the family object here so that this test program will compile/run.
result = {
"family" : "Smith",
"people":{
"Bob":{
"name":"Bob",
"active" : true,
"birthday" : "1/01"
},
"Sue":{
"name": "Sue",
"active" : true,
"birthday" : "1/01"
}
},
"address": {
"street" :"1111 Cool Road",
"city" : "Cincinnati",
"state" : "OH",
"zip" : "11111"
},
"phone" : "923-4908"
};
var body = { "docs": [result]}; // Change #1: docs takes an array.
var stringData = JSON.stringify(body); // Change #2: stringify first.
var postOptions = {
host: 'localhost',
port: '5984',
path: '/members/_bulk_docs',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': stringData.length // Change #3: send the length of the stringified data.
}
};
var request = http.request(postOptions, function(res){
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ' + chunk);
});
});
console.log(stringData);
request.write(stringData);
request.end();
以下是我从node获得的输出:
{"docs":[{"family":"Smith","people":{"Bob":{"name":"Bob","active":true,"birthday":"1/01"},"Sue":{"name":"Sue","active":true,"birthday":"1/01"}},"address":{"street":"1111 Cool Road","city":"Cincinnati","state":"OH","zip":"11111"},"phone":"923-4908"}]}
Response: [{"ok":true,"id":"721b8cde7a1e22b4cc106adbb3f41df9","rev":"1-306b71ff83df48c588a174fd5feafa34"}]
https://stackoverflow.com/questions/13170841
复制相似问题