在运行查找之前,您如何确保流星中的更新已完成? [英] How do you ensure an update has finished in meteor before running a find?

查看:34
本文介绍了在运行查找之前,您如何确保流星中的更新已完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这篇关于在运行查找之前,您如何确保流星中的更新已完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆