【发布时间】:2022-01-23 22:34:33
【问题描述】:
我该如何解决这个问题?我发出了一个post请求来获取token,但是当我返回它时,它返回为未定义,但是当我打印它时,它显示token OK。
function getToken() {
// The req.query object has the query params that
// were sent to this route. We want the `code` param
axios({
// make a POST request
method: "post",
// to the Github authentication API, with the client ID, client secret
// and request token
url: ` https://backstage.taboola.com/backstage/oauth/token?client_id=${clientID}&client_secret=${clientSecret}&grant_type=client_credentials`,
// Set the content type header, so that we get the response in JSOn
headers: {
accept: "application/json",
},
}).then((response) => {
// Once we get the response, extract the access token from
// the response body
accessToken = response.data.access_token;
//console.log(accessToken);
return(accessToken);
});
};
【问题讨论】:
-
你为什么不干脆
return response.data?.access_token? -
并在
axios前面加上return
标签: javascript node.js token