导致Collection.insert的Meteor调用:如何同步运行?

时间:2016-01-16 20:22:24

标签: meteor

我知道如何通过服务器上的Meteor.wrapAsynch和创建new future()同步运行Meteor方法。

但是,当我通过Meteor.call("myMethod")调用方法来插入或删除文档时,我遇到了这个问题。 Collection.insert本质上应该是同步的,但在我的应用程序中,它似乎是异步运行的,因为有时候整个文档没有完全插入数据库,然后铁路由器将我带到下一个页。

以下是我的代码在客户端上的显示方式(大部分内容都是因为它无关紧要):

Template.myTemplate.events({
  "submit .legitForm": function(e){
     e.preventDefault();

    Meteor.call("deleteData");
    });

// Do file manipulation in this space.

      async.each(array, processingFxn, function(error) {

        flattened = _.flatten(results);

        Meteor.call("insertCollect", flattened);
      });

在服务器上,这些方法是通用的Collection.insert并删除:

  Meteor.methods({
    insertColl: function(document){
      var currentUser = Meteor.userId();
      Bank.insert({userId: currentUser, data: document});

    },
    deleteData: function(){
      Bank.remove({userId: this.userId});
    }
  });

理想情况下,我希望在继续执行文件操作部分等之前完成第一次删除调用等。我尝试在Collection.insert中传递回调并删除方法,但它不起作用因为我猜将异步转换插入和删除方法。

1 个答案:

答案 0 :(得分:1)

Meteor文档说如果提供回调,则使用它,否则该方法将被“同步调用”。

为确保订单,您应该在那里使用回调:

    Template.myTemplate.events({
      "submit .legitForm": function(e){
         e.preventDefault();

        Meteor.call("deleteData", function(result) {
          // Do file manipulation in this space.
          async.each(array, processingFxn, function(error) {
            flattened = _.flatten(results);
            Meteor.call("insertCollect", flattened);
        });
    });
});