1

我有一个节点控制台应用程序,我试图将一些记录发布到本地 CouchDB 实例上的批量文档 api 并获得“无效的 UTF-8 JSON”。让这特别奇怪的是,我在对象文字上使用 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"            
};

我将我的记录数组包装在一个带有此处指定的属性“docs”的 json 对象中。我正在尝试做的事情有什么明显的错误吗?

更新:按照lammmj的建议,我将节点生成的字符串写入文件,然后使用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);
4

1 回答 1

4

我能够使用以下命令使您的示例正常工作:

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 与包装在数组中的提供相同,并作为docs传递给 CouchDB 的 JSON 的属性值提供。您给出的示例中还有一个尾随分号 ( ;)。如果其他所有内容都正确格式化,那可能就是问题所在。

更新:

好的,这是答案节点代码。如代码中所述,我进行了 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();

这是我从节点得到的输出:

{"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"}]
于 2012-11-01T15:00:22.703 回答