[英]Create document in arangoDB with node.js
what's the correct way to pass in json documents for creation ? 传递json文档进行创建的正确方法是什么?
I have the example working and ok as below : /* create a new document in collection */ 我有一个工作正常的示例,如下所示:/ *在集合中创建一个新文档* /
db.document.create({a:"test"},function(err,ret){
if(err) console.log("error(%s): ", err,ret);
else console.log(util.inspect(ret));
});
but how do I pass in the json as an argument as this does not work ? 但是如何将json作为参数传递,因为这不起作用?
var json = '{a:"test"}';
db.document.create(json,function(err,ret){
if(err) console.log("error(%s): ", err,ret);
else console.log(util.inspect(ret));
}); });
Looking at the "create" function from Kaerus repository above, the create function is: 查看上面Kaerus存储库中的“创建”功能,创建功能为:
"create": function() {
var collection = db.name, data = {}, options = "", callback, i = 0;
if(typeof arguments[i] === "boolean"){
if(arguments[i++] === true)
options = "&createCollection=true";
}
if(typeof arguments[i] === "string") collection = arguments[i++];
if(typeof arguments[i] === "object") data = arguments[i++];
if(typeof arguments[i] === "function") callback = arguments[i++];
return X.post(xpath+collection+options,data,callback);
},
So you either need to pass it as JavaScript object, that is call 所以您要么需要将其作为JavaScript对象传递,即调用
JSON.parse('{"a":"test"}')
to convert a JSON representation to a JavaScript object or patch Kaerus client to allow an object or string in the line 将JSON表示形式转换为JavaScript对象或补丁Kaerus客户端以允许该行中的对象或字符串
if(typeof arguments[i] === "object") data = arguments[i++];
(this might lead to problems with the optional arguments). (这可能导致可选参数出现问题)。
NOTE: In any case, it is important that "json" contains a valid JSON representation. 注意:无论如何,“ json”包含有效的JSON表示很重要。
{ a: "Test" }
is not valid, 无效,
{ "a": "Test" }
is. 是。
Have a look at this unit test: https://github.com/kaerus/arango-client/blob/master/test/api/document.js 看看这个单元测试: https : //github.com/kaerus/arango-client/blob/master/test/api/document.js
Try 尝试
var json = {"a":"test"};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.