AWS CloudSearch上载JSON:值标记不能是数组或对象

时间:2017-05-12 19:19:36

标签: node.js amazon-web-services amazon-cloudsearch aws-sdk-nodejs

我正在运行lambda函数(NodeJS)将一些文档上传到AWS Cloud Search。我一直收到以下错误。

{
    "errorMessage": "{ [\"The value of tags cannot be a JSON array or object\"] }",
    "errorType": "DocumentServiceException",
    "stackTrace": [
        "Object.extractError (/var/task/node_modules/aws-sdk/lib/protocol/json.js:48:27)",
        "Request.extractError (/var/task/node_modules/aws-sdk/lib/protocol/rest_json.js:37:8)",
        "Request.callListeners (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:105:20)",
        "Request.emit (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:77:10)",
        "Request.emit (/var/task/node_modules/aws-sdk/lib/request.js:678:14)",
        "Request.transition (/var/task/node_modules/aws-sdk/lib/request.js:22:10)",
        "AcceptorStateMachine.runTo (/var/task/node_modules/aws-sdk/lib/state_machine.js:14:12)",
        "/var/task/node_modules/aws-sdk/lib/state_machine.js:26:10",
        "Request.<anonymous> (/var/task/node_modules/aws-sdk/lib/request.js:38:9)",
        "Request.<anonymous> (/var/task/node_modules/aws-sdk/lib/request.js:680:12)"
    ]
}

我遵循了

的文档格式
var item = {
  type: 'add',
  id: key,
  fields: {
      userid: value.userId,
      storyid: value.storyId,
      description: value.description,
      title: value.title,
      type: 'xyz'
  }
}

这是我用来上传数据的代码

exports.handle = function(e, ctx, cb) {
  ctx.callbackWaitsForEmptyEventLoop = false;
  var documentsBatch = e.data;
  var params = {
    contentType: 'application/json',
    documents: JSON.stringify(documentsBatch)
  };
  var req = cloudsearchdomain.uploadDocuments(params, function(err, data) {
    if (err){
      // an error occurred
      cb(err, null);
    }else{
      // successful response
    }
  });
  req.send();
}

记录时我的字符串化数据类似于

[
  {
    "type": "add",
    "id": "FpgAxxxxKrM4utxosPy23--KhO6FgvxK",
    "fields": {
      "userid": "FpgARscKlxaxutxosPy23",
      "storyid": "-KhxbPpRP7REEK",
      "description": "xyz  ",
      "title": "umm",
      "type": "story"
    }
  },
  {
    "type": "add",
    "id": "FccccxosPy23--KiYbrrPjtJVk2bghO-W",
    "fields": {
      "userid": "FpgARfPy23",
      "storyid": "-KiYbrfggO-W",
      "description": "noo",
      "title": "lalaa out",
      "type": "story"
    }
  }
]

有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:1)

问题出在另一个JSON对象上,除了字段之外还有一个额外的JSON属性。一旦我能够找到并删除它一切正常。应该有一个lint-er,或SDK应该抛出一个更好的例外。