Meteor collection.insert callback issues

According to the Meteor documentation ....

collection.insert(doc, [callback])

callback function

Additionally. If present, is called with the error object as the first argument, and _id as the second.

... then down ...

On the server, if you do not provide a callback, insert blocks until the database recognizes the record, or throw an exception if something goes wrong. If you call back, the insert returns immediately. After the insert completes (or fails), the callback is called with arguments with an error and the result is the same as for the methods.

What is it, error and _id or error and result? I have Meteor.methods that run their callbacks correctly with an error, the result is available for the scope.

I just can't get callback to work correctly in collection.insert (doc, [callback])

Anyway, I can’t get my callback to register anything?

 function insertPost(args) { this.unblock; if(args) { post_text = args.text.slice(0,140); var ts = Date.now(); Posts.insert({ post: post_text, created: ts }, function(error, _id){ // or try function(error, result) and still get nothing // console.log('result: ' + result); console.log('error: ' + error); console.log('_id: ' + _id); //this._id doesn't work either }); } return; } 

What am I doing wrong? I have been since I coded ... 18:00 my time zone ... I am vague, so I could (possibly) miss something completely obvious.

Cheers Steeve

+7
source share
2 answers

This was a bug fixed in the next version. Now, if you provide the insert callback, it will be called with the arguments error and result , where result is the identifier of the new document, or null if there is an error.

+6
source

Since this is server code, you can simply:

 var id = Posts.insert({data}); // will block until insert is complete 

and the id will be available.

+5
source

All Articles