等待客户端上的Meteor.call结果

时间:2016-11-14 14:39:50

标签: javascript meteor

我是JavaScript新手。我不了解如何等待Meteor.call方法的结果。     这是我的代码

     //client/main.js
     //Added the callback
        Template.hello.events({
  'click button'(event, instance) {
    // increment the counter when button is clicked
    instance.counter.set(instance.counter.get() + 1);
    var res = Meteor.call("callMeLater","sanj",function (err,res) {
      if (err) {
        console.log(err);
      } else {
        console.log("this is the result main ", res);
      }
    });
    console.log("this is the result ", res);
  }
        //server/main.js
        Meteor.methods({
        callMeLater :function (name) {
            var callMeLaterSync =Meteor.wrapAsync(callMeLaterAsync);
            var result = callMeLaterSync(name);
            console.log("this is the test", result);
            return result;
        }
    });


    var callMeLaterAsync = function (name,cb) {
        setTimeout(function () {
            cb && cb (null ,"hey there, "+name);
        },2000);
    };

在控制台上,我得到了

this is the result  undefined

this is the result main  hey there, sanj

如何通过阻止客户端的执行来等待Meteor.call的结果。 请帮忙

谢谢

1 个答案:

答案 0 :(得分:0)

只需将代码放入回调方法即可。

   Meteor.call('callMeLater',"sanj", function(err, res){
    if (err) {
      console.log(err);
    } else {
      console.log("this is the result ", res);
    }
  });