Meteor methods wait before execution on client proceeds -
could please tell me how make clients wait until called function on server executed?
my code:
meteor.methods({ marklettersasread: function(userid) { if(servervar) { users.update({_id: userid}, {$set: {letters: []}}); // removing references } }
});
template.letter.events({ 'click a': function() { meteor.call('marklettersasread', meteor.userid(), this._id, function(err) { if (err) { console.log(err); } }); var usersexistswiththisletter = users.find({letters: {_id: this._id}}).count(); console.log(usersexistswiththisletter); }
});
in example usersexistswiththisletter
1 because users.find()
doesn't wait until meteor.call
done. verified checking database , no users exists entries in letters
array.
any appreciated.
you need query collection inside callback, because can server method has been executed. (note self
variable declaration):
var self = this; meteor.call('marklettersasread', meteor.userid(), this._id, function(err) { if (!err) { var usersexistswiththisletter = users.find({letters: {_id: self._id}}).count(); console.log(usersexistswiththisletter); } else { console.log(err); } });
i hope helps!
Comments
Post a Comment