在运行查找之前,您如何确保流星中的更新已完成?

问题描述:

我有一个相当典型的需求,即确保在运行查找之前已完成插入/更新.代码是这样的:

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...

虽然在大多数情况下,这可以作为异步调用,但随着 dom 在事情完成时更新,在我的情况下,插入和更新必须在我运行函数调用之前完成.

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?

目前我有类似的东西:

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.

这不就是可选回调参数的用途吗?

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}});
                                   }
                                  );
                });