在下面的代码中,在完成后请求执行之前,将响应发送到端点。
function(req, res) {
var details;
getPublisherDetails() {
getPublisherDetailsFromServer();
}
res.send(details);
}
getPublisherDetailsFromServer() {
var det;
const JSON = require('circular-json');
var Client = require('node-rest-client').Client;
var client = new Client();
var args = {
data: {
grant_type: "client_credentials",
client_id: "mk",
client_secret: "Mank8"
},
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
};
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
client.post(cache.getURL(), args, function(data, response) {
console.log("Response from server-->" + JSON.stringify(data));
det = data[Object.keys(data)[0]];
});
return det;
}
除非邮寄申请完成,否则det
以上不包含任何内容
答案 0 :(得分:2)
这就是js异步的工作方式。您需要在client.post
回调
function(req,res) {
const JSON = require('circular-json');
var Client = require('node-rest-client').Client;
var client = new Client();
var args = {
data:
{grant_type:"client_credentials",client_id:"mk"
,client_secret:"Mank8"},
headers: {"Content-Type": "application/x-www-form-urlencoded"}
};
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
client.post(cache.getURL(),args,function (data, response) {
console.log("Response from server-->"+JSON.stringify(data));
var det = data[Object.keys(data)[0]];
res.send(det);
});
}