Node.js 在封闭方法中访问节点JS Express请求中的响应后数据

Node.js 在封闭方法中访问节点JS Express请求中的响应后数据,node.js,express,callback,request,Node.js,Express,Callback,Request,我在这里直接进入了一个学习曲线。我在Node JS Express中有一个应用程序,它使用request向另一个API发布帖子。通话进行得很顺利,我的控制台日志显示返回的数据是正确的。我的问题是如何从request.post(…)中获得响应,并在发出请求的方法中使用它 这是我的设想。外部应用程序调用我的API。我的API必须调用另一个API来获取一些数据以检查更新。(我有一个API发出POST请求,以便响应来自外部应用程序的POST请求。) 这是我的API中向第三方请求某些数据的方法。我需要从这

我在这里直接进入了一个学习曲线。我在Node JS Express中有一个应用程序,它使用request向另一个API发布帖子。通话进行得很顺利,我的控制台日志显示返回的数据是正确的。我的问题是如何从request.post(…)中获得响应,并在发出请求的方法中使用它

这是我的设想。外部应用程序调用我的API。我的API必须调用另一个API来获取一些数据以检查更新。(我有一个API发出POST请求,以便响应来自外部应用程序的POST请求。)

这是我的API中向第三方请求某些数据的方法。我需要从这个POST响应中获取数据,在将其返回到外部应用程序的POST请求中之前对其进行处理

exports.getUpdates = function(variable1, variable2, variable3) {
     request.post(
        'http://myurl.exp/api//getUpdates',
        {json: {var1: variable1, ...}},
        function (error, response, body) {
           if(!error && response.statusCode == 200) {
             console.log(body);
           } else {console.log(error);}
        }
     );
  <I need to have this method return the response to the controller that called this method>
 };
exports.getUpdates=函数(variable1、variable2、variable3){
请寄(
'http://myurl.exp/api//getUpdates',
{json:{var1:variable1,…}},
功能(错误、响应、正文){
如果(!error&&response.statusCode==200){
控制台日志(主体);
}else{console.log(错误);}
}
);
};
我看过很多例子,但它们都说console.log(),我越来越讨厌它了。我猜这与回调以及我如何正确处理有关,但我的研究都没有给我一个明确的解决方法 处理回叫。感谢您的帮助。

请使用回调

exports.getUpdates = function(variable1, variable2, variable3, callback) {
     request.post(
        'http://myurl.exp/api//getUpdates',
        {json: {var1: variable1, ...}},
        function (error, response, body) {
           if(!error && response.statusCode == 200) {
             callback(error, response, body);
           } else {console.log(error);}
        }
     );
 };
现在,您可以在调用此函数时传递回调:

getUpdates(var1, var2, var3, function(error, response, body) {
    //stuff that you want to perform after you get response or error
});
getUpdates(var1, var2, var3, function(error, response, body) {
        if(!error && response.statusCode == 200) {
            // stuff you want to do
        } else {
            console.log(error);
        }
    }
});
但是,我建议采用更干净的方法:

exports.getUpdates = function(variable1, variable2, variable3, callback) {
     request.post('http://myurl.exp/api//getUpdates', 
                     {json: {var1: variable1, ...}}, callback);
 };
现在,您可以在调用此函数时传递回调:

getUpdates(var1, var2, var3, function(error, response, body) {
    //stuff that you want to perform after you get response or error
});
getUpdates(var1, var2, var3, function(error, response, body) {
        if(!error && response.statusCode == 200) {
            // stuff you want to do
        } else {
            console.log(error);
        }
    }
});

可能重复的谢谢,这正是我需要的,它工作得很好。我最后看到的是您将回调主体分配给原始响应的结果。在原始请求路由处理程序中:exports.getUpdates=function(req,res){event.getUpdates(req.body.var1,req.body.var2,req.body.var3,function(error,response,body){if(!error&&response.statusCode==200){res.json(body);}else{console.log(error);}}}};在事件控制器中:exports.getUpdates=函数(variable1,variable2,variable3,回调){request.post('',{json:{var1:variable1,…},回调)};这将主体JSON返回到外部应用程序,这表明它正在工作,现在我可以获得真正的代码了。谢谢Sandesh!