user1305541
user1305541

Reputation: 205

Create document in arangoDB with node.js

what's the correct way to pass in json documents for creation ?

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 ?

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));

});

Upvotes: 4

Views: 1113

Answers (2)

herrjeh42
herrjeh42

Reputation: 2793

Have a look at this unit test: https://github.com/kaerus/arango-client/blob/master/test/api/document.js

Try

 var json = {"a":"test"};

Upvotes: 2

fceller
fceller

Reputation: 2764

Looking at the "create" function from Kaerus repository above, the create function is:

"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

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

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.

{ a: "Test" }

is not valid,

{ "a": "Test" }

is.

Upvotes: 4

Related Questions