Meteor.call不会等待结果

时间:2016-12-11 10:36:23

标签: javascript meteor

Meteor.call中的Async回调不会等待Meteor.method的结果。这是代码。

Meteor.call("fetchData",function (err,res) {
        if (err){
            console.log("error ", err);
        }else {
            console.log("success ", res);
            return res;
        }
    });//calling this from onRendered of client/somejs.js 

这是方法

fetchData :function(){
        HTTP.call("POST","http://localhost:8080",{
            data:'{"apple":"grape"}'
        },function (err,res) {
            if (err){
                console.log("error ", err);
            }else {
                console.log("success ", res);
                return res;
            }
        })
    }//Server/methods.js

当触发Meteor.call时,我会在服务器上以[{1}}的形式登录其结果。 在客户端,我得到success。 客户端上的调用不等待结果。我也在服务器上尝试了光纤和同步执行。它对我不起作用。在这种情况下,发布被阻止(我猜是由于API调用)。

另一件事是我尝试使用数据库查询而不是API调用。这很好。我从方法中得到结果。

我哪里出错。帮助。

由于

1 个答案:

答案 0 :(得分:1)

Sanjith。

你正走在正确的道路上。默认情况下,Meteor的方法是异步的,因此客户端需要一些“等待”机制。为此,我建议使用Meteor.wrapAsync或Promises。以下是关于实施两者的两个详细解释:

https://themeteorchef.com/snippets/synchronous-methods/#tmc-using-wrapasync

https://themeteorchef.com/snippets/promise-based-modules/#tmc-calling-our-promise-based-module-from-the-client

第二个链接更侧重于使用promises构建代码,但提供了一个很好的演示,说明如何调用依赖Promise响应的方法。

相关问题