流星方法不返回错误或结果值 [英] Meteor method not returning error or result value

查看:43
本文介绍了流星方法不返回错误或结果值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的服务器端代码

Meteor.methods({
   addSupportRequest: function (support) {
    Support.insert(support, function (err, id) {
        if (err)
            throw new Meteor.Error(404, "Oops! Network Error. Please submit help request again. ");

            console.log("Support resuest added: " + id);
           return id;

    });
} // End addSupportRequest

});

这里是客户端代码

App.Util = {
call: function (method, params, callback) {
    NProgress.start();
    Meteor.apply(method, params, function (error, result) {
        NProgress.done();

       console.log(error);
        console.log(result);
        callback(error, result);

    });
} // end Call

};

请帮我找出为什么meteor 方法调用没有返回错误或结果.console.log() 在服务器上显示新记录 ID,但在客户端显示未定义.

Please help me find out why meteor method call is not returning error or result. console.log() showing new record Id on server but showing undefined on client.

推荐答案

Dsyko 的回答在某种程度上是正确的.但是,异步回调永远不会将其结果传递给已经结束的原始函数的作用域.

Dsyko's answer was somewhat on the right track. However, the asynchronous callback will never pass its result to the scope of the original function, which has ended.

您想要的是同步运行Support.insert操作,即在I/O发生时获得当前的光纤产量.这就是 Meteor._wrapAsync 函数的用途.幸运的是,您无需手动执行此操作,因为如果您只是从插入操作中取出回调,它将同步运行:

What you want is to run the Support.insert operation synchronously, i.e. having the current fiber yield while I/O is happening. This is what the Meteor._wrapAsync function is for. Luckily for you, there is no need to do this manually because if you just take out the callback from the insert operation, it will run synchronously:

Meteor.methods({
   addSupportRequest: function (support) {
     var id = Support.insert(support);
     // An error may be thrown here, but it certainly won't be a network error because the request will have already reached the server.
     console.log("Support request added: " + id);
     return id;
   });
}

这篇关于流星方法不返回错误或结果值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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