How do you ensure an update has finished in meteor before running a find?

…衆ロ難τιáo~ 提交于 2019-12-25 06:59:09

问题


I have a fairly typical need, to ensure an insert / update has finished before I run a find. The code goes something like this:

//Update my collections
Messages.insert({author:_id,text:text});
Authors.update({_id:_id},{$inc:{messages:1}});

//Wait until the update / insert has finished

//Perform some actions on the collections just updated
var author = Authors.findOne({task:taskId},{sort:{'messages':1}});
//Do some more complex stuff...

While in most cases this would be fine as asynchronous calls, with the dom updating as and when things complete, in my case it is essential that the insert and update have completed before I run the function call.

Do I need to perform the insert and update as a server side call with a callback function, or is there some way I could do this on the client side?

At the moment I have something like:

Meteor.call("recordMessage", _id, text, 
    function(err, out){postMessage(_id)}
);

which works - but I'd like to know if I could do this on the client side.


回答1:


Isn't that what the optional callback arguments are for?

var author;
Messages.insert({author:_id, text:text},
                function(err, result) {
                    Authors.update({_id: result},
                                   {$inc: {messages:1}},
                                   function(err, result) {
                                       author = Authors.findOne({task:taskId}, 
                                                                {sort:{'messages':1}});
                                   }
                                  );
                });


来源:https://stackoverflow.com/questions/20597012/how-do-you-ensure-an-update-has-finished-in-meteor-before-running-a-find

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!