【发布时间】:2021-07-22 13:36:57
【问题描述】:
我正在尝试访问 Microsoft Azure 管理 API,并且已经到了可以 console.log() 来自我的 axios 调用的数据的地步。
我不知道该怎么做,如何将我的 axios 调用包装在一个函数中,然后稍后检索该数据?
我的代码如下。请注意,我在控制台中看到了从 MS api 返回的数据,正如我所期望的那样。
// Express setup code, module imports, env variables, etc.
// ...
function getAzureToken() {
// Set up credentials, auth url and request body, axios config
// The API authenticates properly, so it is not relevant to include
// ...
var data = {};
axios.post(authEndpoint, config)
.then(result => {
console.log(result.data) // Logs the expected data (auth token) in my console
data = result.data
})
return data
}
// Index API endpoint
app.get('/', (req, res) => {
let token = getAzureToken()
// Prints out this in my browser: {}
res.send(token)
})
任何人都可以帮助菜鸟吗?
【问题讨论】:
标签: javascript node.js express axios