4

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

});

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

My data is in a stream so its a buffer. I converted that to a string and then did a JSON.parse on that. This doesn't work, I get back a '404' response. the JSON is valid (the output from a 'console.log' on the buffer tested here:jsonlint.com). If I pass in the data directly it works fine...very confusing...
Ok, that is strange. 404 does not mean "corrupt JSON", but "collection not found". Can you try the following: db.document.create("COLLECTIONNAME",json,function(err,ret){ if(err) console.log("error(%s): ", err,ret); else console.log(util.inspect(ret)); where COLLECTIONNAME is the name of the collection you are using?
I've tried than and the 404 error is fixed. its creating a document but without any of the passed in json data, i just have the _id, _key and_rev fields populated. the callback of the 'create' does not get called so there is no output from the 2 console lines.
2

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

Try

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

2 Comments

Thankyou, I've had a brainstorm ! I'm calling the arango client function with stream data which will be a buffer and I've translated it to a string which is clearly not accepted, as per your comment. I guess what I need to know is how do I get from a string (or buffer) to the format you have suggested.
What about JSON.parse(yourJsonString); ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.