【发布时间】:2019-03-11 19:28:25
【问题描述】:
在良好的云函数环境中,在 request1 调用中,我可以得到 bodydayOfTheWeek 的输出。
但在 request1 调用之外,我得到未定义的输出。
我不知道为什么。即使 let 变量 bodydayOfTheWeek 是在 request1 调用之外声明的。
所以不确定范围变量在请求调用中是如何工作的
const request1 = require('request');
const url = 'http://worldclockapi.com/api/json/pst/now';
let bodydayOfTheWeek: any;
request1(url, { json: true }, (err: any, res: any, body: any) => {
if (err) { return console.log(err); }
console.log('inside day of the week');
bodydayOfTheWeek = body.dayOfTheWeek;
console.log(bodydayOfTheWeek); <-- works
});
console.log('outside day of the week');
console.log(bodydayOfTheWeek); <-- undefined
【问题讨论】:
标签: node.js typescript request google-cloud-functions httpclient