Is there any possibility of calling a server method from within a observe
callback in Meteor?
I put together an example that reproduces the issue, that
This might be a known issue, i'm not sure since I've not tried it myself, but it looks like there might be a workaround (see https://github.com/meteor/meteor/issues/907)
Add your Meteor.call
into an instantaneous setTimeout callback:
added: function(doc) {
console.log("added "+doc.text);
setTimeout(function() {
Meteor.call('aMethod',doc.text,function(e,r){
if(e){
console.log("error from server: "+e);
}else{
console.log("response from server: "+r);
}
});
},0);
}
Btw Tarang's answer also works when calling from a client method. Essentially I have a file in the client scope. And this file is a method definition. Inside my method definition I call a method at the server.
I was having problems because only the client stub of the server call is being called and the server method does not get called. The fix was this one:
Meteor.methods({
assignUserSession: function(options) {
setTimeout(function() { // https://stackoverflow.com/questions/18645334/meteor-meteor-call-from-within-observe-callback-does-not-execute
// Find all the base_accounts of this user and from there decide which view to prioritize.
Meteor.call('user_base_accounts', {user_id: Meteor.userId()}, function(error,result){
....
//server is then called due to logs shown in the command prompt
....
});
});
}
});
Thanks for pointing this out!