从API调用捕获的NodeJS响应
我是新来的节点JS。我想调用的API,这将使我一个网址,然后我会用这个URL做一些事情像发送短信,电子邮件,插入到MySQL数据库。
API调用工作正常,当我登录请求块内的URL(见与做工精细的注释代码)
但是,当我尝试登录请求块外部的URL,这是行不通的。 (请参阅使用不工作FINE注释代码)。这在我想要做的所有事情URL。
请帮忙。谢谢
var body =
{
"mobile": Mobile,
"policy": Policy_number,
"name": Policy_Holder_Name,
"docs": Docs_Pending,
"target": 'mobile'
};
var options =
{
uri: BaseURL,
method: 'POST',
headers: header,
json: body
};
var URL = '';
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
URL = response.body.url;
console.log(URL); //WORKING FINE
}
});
console.log(URL); //NOT WORKING FINE
回答如下:
当别人说出你的问题是在你的代码执行顺序。
到请求该调用声明上即时一旦接收到响应将仅被执行(称为lambda函数)的函数。然后执行将继续进行到底,然后运行该功能。
我的评论下面的代码,试图解释。
/**THIS CODE IS RUN FIRST**/
var body = {
"mobile": Mobile,
"policy": Policy_number,
"name": Policy_Holder_Name,
"docs": Docs_Pending,
"target": 'mobile'
};
var options = {
uri: BaseURL,
method: 'POST',
headers: header,
json: body
};
var URL = '';
/**DECLARES A FUNCTION BUT IS NOT RUN UNTIL THE REQUEST COMPLETES**/
request(options, function (error, response, body) {
/**THIS CODE RUNS LAST**/
if (!error && response.statusCode == 200) {
URL = response.body.url;
console.log(URL); //WORKING FINE
}
});
/**RUNS SECOND - URL IS STILL NOT DEFINED**/
console.log(URL); //NOT WORKING FINE
如果你想运行一些代码后,那么最好的方式可能是把它放在一个函数。
request(options, function (error, response, body) {
/**THIS CODE RUNS LAST**/
if (!error && response.statusCode == 200) {
doMoreStuff(response.body.url);
}
});
function doMoreStuff(URL){
console.log(URL);
}
从API调用捕获的NodeJS响应
我是新来的节点JS。我想调用的API,这将使我一个网址,然后我会用这个URL做一些事情像发送短信,电子邮件,插入到MySQL数据库。
API调用工作正常,当我登录请求块内的URL(见与做工精细的注释代码)
但是,当我尝试登录请求块外部的URL,这是行不通的。 (请参阅使用不工作FINE注释代码)。这在我想要做的所有事情URL。
请帮忙。谢谢
var body =
{
"mobile": Mobile,
"policy": Policy_number,
"name": Policy_Holder_Name,
"docs": Docs_Pending,
"target": 'mobile'
};
var options =
{
uri: BaseURL,
method: 'POST',
headers: header,
json: body
};
var URL = '';
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
URL = response.body.url;
console.log(URL); //WORKING FINE
}
});
console.log(URL); //NOT WORKING FINE
回答如下:
当别人说出你的问题是在你的代码执行顺序。
到请求该调用声明上即时一旦接收到响应将仅被执行(称为lambda函数)的函数。然后执行将继续进行到底,然后运行该功能。
我的评论下面的代码,试图解释。
/**THIS CODE IS RUN FIRST**/
var body = {
"mobile": Mobile,
"policy": Policy_number,
"name": Policy_Holder_Name,
"docs": Docs_Pending,
"target": 'mobile'
};
var options = {
uri: BaseURL,
method: 'POST',
headers: header,
json: body
};
var URL = '';
/**DECLARES A FUNCTION BUT IS NOT RUN UNTIL THE REQUEST COMPLETES**/
request(options, function (error, response, body) {
/**THIS CODE RUNS LAST**/
if (!error && response.statusCode == 200) {
URL = response.body.url;
console.log(URL); //WORKING FINE
}
});
/**RUNS SECOND - URL IS STILL NOT DEFINED**/
console.log(URL); //NOT WORKING FINE
如果你想运行一些代码后,那么最好的方式可能是把它放在一个函数。
request(options, function (error, response, body) {
/**THIS CODE RUNS LAST**/
if (!error && response.statusCode == 200) {
doMoreStuff(response.body.url);
}
});
function doMoreStuff(URL){
console.log(URL);
}
与本文相关的文章
评论列表 (0)
- 暂无评论