传递json文档进行创建的正确方法是什么?
我的示例正常,如下所示: / *在集合* /
中创建一个新文档db.document.create({a:"test"},function(err,ret){
if(err) console.log("error(%s): ", err,ret);
else console.log(util.inspect(ret));
});
但是如何将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));
});
答案 0 :(得分:4)
查看上面Kaerus存储库中的“create”函数,create函数是:
"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);
},
所以你需要将它作为JavaScript对象传递,即调用
JSON.parse('{"a":"test"}')
将JSON表示转换为JavaScript对象或修补Kaerus客户端 允许行中的对象或字符串
if(typeof arguments[i] === "object") data = arguments[i++];
(这可能会导致可选参数出现问题)。
注意:在任何情况下,“json”包含有效的JSON表示非常重要。
{ a: "Test" }
无效,
{ "a": "Test" }
是
答案 1 :(得分:2)
看一下这个单元测试:https://github.com/kaerus/arango-client/blob/master/test/api/document.js
尝试
var json = {"a":"test"};