我有一个执行POST请求以从API获取访问令牌的函数。该功能在将令牌打印到控制台的范围内有效,但是我找不到解析访问令牌并将其保存为变量以供其他方法使用的方法。
我在Stack Overflow上进行了很多搜索,遇到的一件事是回调的概念async / await ...但是没有具体的答案可以告诉我如何在请求后返回值制造。
我得到未定义的值,可能是因为我的函数在请求结束之前执行得太快了,但是我还是想知道一个特定的代码实现(使用另一个回调函数或其他方法),它使我能够访问该值这些异步功能之外。
下面是我的代码和屏幕截图。 请记住,这是Alexa技能的Lambda代码
Contour with children: #124 r=[2190 x 827 from (1, 799)] h=[213, 123, 125, -1]
* Child contour: #125 r=[377 x 99 from (1808, 1521)] h=[127, -1, 126, 124]
* Child contour: #127 r=[377 x 98 from (1808, 1419)] h=[131, 125, 128, 124]
* Child contour: #131 r=[377 x 98 from (1808, 1317)] h=[133, 127, 132, 124]
* Child contour: #133 r=[377 x 98 from (1808, 1214)] h=[135, 131, 134, 124]
* Child contour: #135 r=[607 x 98 from (1197, 1214)] h=[137, 133, 136, 124]
* Child contour: #137 r=[943 x 98 from (250, 1214)] h=[144, 135, 138, 124]
* Child contour: #144 r=[243 x 99 from (3, 1214)] h=[146, 137, 145, 124]
* Child contour: #146 r=[377 x 98 from (1808, 1112)] h=[148, 144, 147, 124]
* Child contour: #148 r=[607 x 98 from (1197, 1112)] h=[150, 146, 149, 124]
* Child contour: #150 r=[943 x 98 from (250, 1112)] h=[156, 148, 151, 124]
* Child contour: #156 r=[243 x 98 from (3, 1112)] h=[158, 150, 157, 124]
* Child contour: #158 r=[377 x 99 from (1808, 1009)] h=[160, 156, 159, 124]
* Child contour: #160 r=[607 x 99 from (1197, 1009)] h=[162, 158, 161, 124]
* Child contour: #162 r=[943 x 99 from (250, 1009)] h=[171, 160, 163, 124]
* Child contour: #171 r=[243 x 99 from (3, 1009)] h=[173, 162, 172, 124]
* Child contour: #173 r=[377 x 98 from (1808, 907)] h=[174, 171, -1, 124]
* Child contour: #174 r=[607 x 98 from (1197, 907)] h=[175, 173, -1, 124]
* Child contour: #175 r=[943 x 98 from (250, 907)] h=[182, 174, 176, 124]
* Child contour: #182 r=[243 x 100 from (3, 907)] h=[183, 175, -1, 124]
* Child contour: #183 r=[377 x 98 from (1808, 805)] h=[187, 182, 184, 124]
* Child contour: #187 r=[607 x 98 from (1197, 805)] h=[199, 183, 188, 124]
* Child contour: #199 r=[943 x 98 from (250, 805)] h=[208, 187, 200, 124]
* Child contour: #208 r=[243 x 98 from (3, 805)] h=[-1, 199, 209, 124]
答案 0 :(得分:0)
解析令牌
一旦您拥有代表令牌对象的文本,就可以使用JSON.parse
将其转换为对象:
var json = '{"token":"ABCDEFG123456789", "expires":36000}';
obj = JSON.parse(json);
console.log(obj.token);
// expected output: ABCDEFG123456789
使用回调
您可以将回调函数作为参数发送给httpRetrieveToken函数,例如:
var http = require("https");
function httpRetrieveToken(cb) {
var options = {
"method": "POST",
"host": "hostName",
"path": "pathVal",
"headers": {
"content-type": "contentType",
"Authorization": "Bearer token",
"cache-control": "no-cache",
}
};
var req = http.request(options, function (res) {
var _cb = cb;
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
var token = JSON.parse(body.toString());
_cb(token); // Callback function
});
});
req.write("stuff here");
req.end();
}
然后您可以使用以下功能:
httpRetrieveToken(function(token) {
// Do something with the token
});
使用回调函数的原因是在异步进程完成后立即执行它,然后在该回调中将进程包含所需的数据。
使用Promise
使用promise对象,您可以提供一个函数,该函数可以在过程成功时执行resolve,在出错时拒绝。然后,您可以使用then
方法设置一个要成功执行的函数,您可以在其中使用http请求检索的令牌。
var http = require("https");
function httpRetrieveToken() {
var options = {
"method": "POST",
"host": "hostName",
"path": "pathVal",
"headers": {
"content-type": "contentType",
"Authorization": "Bearer token",
"cache-control": "no-cache",
}
};
return new Promise(function(resolve, reject) {
let req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
var token = JSON.parse(body.toString());
resolve(null, token); // Callback function
});
});
req.on("error", function(err) {
reject(err);
});
});
}
httpRetrieveToken().then((err, token) => {
if(err) console.log(err);
// Do something with token
});