使用node.js通过http api导入Arangodb

使用node.js通过http api导入Arangodb,node.js,arangodb,Node.js,Arangodb,我正在尝试使用http api将json数组从各种节点模块(如Pinele、http和request)导入arangodb。每次出现以下错误或类似情况时: { error: true, errorMessage: 'expecting a JSON array in the request', code: 400, errorNum: 400 } 代码如下所示(与上面列出的大多数模块类似,但有微小的变化)。各种场景(单文档导入等)似乎都指向由于某种原因无法正确识别帖子正文 var

我正在尝试使用http api将json数组从各种节点模块(如Pinele、http和request)导入arangodb。每次出现以下错误或类似情况时:

{ error: true,
  errorMessage: 'expecting a JSON array in the request',
  code: 400,
  errorNum: 400 }
代码如下所示(与上面列出的大多数模块类似,但有微小的变化)。各种场景(单文档导入等)似乎都指向由于某种原因无法正确识别帖子正文

var needle = require('needle');

var data = [{
    "lastname": "ln",
    "firstname": "fn",
},
{
    "lastname": "ln2",
    "firstname": "fn2"
}];

var options = {  'Content-Type': 'application/json; charset=utf-8'  };


needle.request('POST', 'http://ip:8529/_db/mydb/_api/import?type=array&collection=accounts&createCollection=false', data, options,     function(err, resp) {
    console.log(resp.body);
});
虽然我能够使用curl和浏览器开发工具上传文档,但我无法在node.js中使用它。我做错了什么?这让我快发疯了。任何帮助都将不胜感激。非常感谢。

您可以使用ngrep(或wireshark)快速找出问题所在:

ngrep -Wbyline port 8529 -d lo
T 127.0.0.1:53440 -> 127.0.0.1:8529 [AP]
POST /_db/mydb/_api/import?type=array&collection=accounts& createCollection=true HTTP/1.1.
Accept: */*.
Connection: close.
User-Agent: Needle/0.9.2 (Node.js v1.8.1; linux x64).
Content-Type: application/x-www-form-urlencoded.
Content-Length: 51.
Host: 127.0.0.1:8529.
.
##
T 127.0.0.1:53440 -> 127.0.0.1:8529 [AP]
lastname=ln&firstname=fn&lastname=ln2&firstname=fn2
发送给ArangoDB的主体必须是json(正如您试图通过设置内容类型来实现的那样)。 使指针实际发布json的工作方式如下:(请参阅)

这就产生了正确的回答:

{ error: false,
  created: 2,
  errors: 0,
  empty: 0,
  updated: 0,
  ignored: 0 }
{ error: false,
  created: 2,
  errors: 0,
  empty: 0,
  updated: 0,
  ignored: 0 }