I need to retrieve the _id after insert a document.
In client:
Meteor.call('saveDocument', value1, value2);
In server
saveDocument: function (value1, value2) {
MyCollection.insert({ 'value1': value1, 'value2': value2});
}
I have tried with the callback function of the insert in the server side. This way I can get the document's _id, but inside the callback function and this can't return to the client call:
saveDocument: function (value1, value2) {
MyCollection.insert({ 'value1': value1, 'valu2': value2},
function(err, docsInserted){ console.log(docsInserted) });
//Works, but docsInserted can't return to the client.
}
your client call should use the async style - from the docs
On the client, if you do not pass a callback and you are not inside a stub, call will return undefined, and you will have no way to get the return value of the method.
Meteor.call('saveDocument', value1, value2, function(error, result){
var theIdYouWant = result;
});
then you just return the id from the method
saveDocument: function (value1, value2) {
return MyCollection.insert({ 'value1': value1, 'valu2': value2});
}
for good measure give a once over to these 2 sections of the docs
http://docs.meteor.com/#meteor_call
http://docs.meteor.com/#insert
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With