【发布时间】:2020-06-21 06:26:32
【问题描述】:
我对 Nodejs 有点陌生。 在以下代码中,我从 API 获取数据。
request.post({ url:endpoint, form: requestParams }, function (err, response, body) {
if (err) {
console.log("Error = " + err);
}
else {
let parsedBody = JSON.parse(body);
if (parsedBody.error_description) {
console.log("Error = " + parsedBody.error_description);
}
else {
// testFunc(AccessToken);
test = "Success!";
testFunc(test);
}
}
});
function testFunc(success){
Token = success;
console.log(Token);
}
// this code gives the error "Token is not defined" \/
console.log(Token);
在发布请求中,我将变量设为“test”。我希望能够将其用作全局变量,以便在获取请求中使用它。
当我在“testFunc”中console.log()“Token”时,它会正确记录它。
但是当我在函数之外console.log() 时,它会给出错误Token is not defined。
如何将“Token”或“test”变量设为全局变量,以便在另一个 get 请求中使用它?
提前致谢!
【问题讨论】:
标签: node.js variables post scope global-variables