Meteor.http.call:如何将Meteor.http.call的结果分配给服务器端的变量?

时间:2017-07-28 05:41:43

标签: meteor

我有以下代码:

Meteor.http.call("POST","/abcd/efd",{data:payload}, function (error,result) {
    if (error){
        //...
    } else { 
        myresult=result
    }
});
console.log("myresult-->",myresult) //myresult is undefined here

由于代码在服务器端,我无法使用会话。

1 个答案:

答案 0 :(得分:0)

您正在调用异步函数,这就是

的原因
console.log("myresult-->",myresult) //myresult is undefined here
无论是否收到结果,都会在Meteor.http.call执行后立即调用

但是,由于您位于服务器端,因此可以轻松调用http:

的同步版本
import { HTTP } from 'meteor/http'

const myresult = HTTP.call("POST", "/abcd/efd", {data:payload});
console.log("myresult-->" my result) //myresult is defined here

更多内容请继续阅读:https://docs.meteor.com/api/http.html#HTTP-call

相关问题