我正在尝试访问,我已经到了可以从axios调用中console.log()数据的地步。
我不知道该如何做,如何将我的axios调用封装在一个函数中,然后再检索数据呢?
我的密码在下面。注意,我在控制台中看到了从MS返回的数据,就像我预期的那样。
// 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)
})有人能伸出援助之手吗?
发布于 2021-07-22 13:38:26
您需要在.then方法中返回以提取值。
// Express setup code, module imports, env variables, etc.
// ...
async function getAzureToken() {
// Set up credentials, auth url and request body, axios config
// The API authenticates properly, so it is relevant to include
// ...
return await axios.post(authEndpoint, config)
.then(result => {
console.log(result.data) // Logs the expected data (auth token) in my console
return result.data
})
}
// Index API endpoint
app.get('/', async (req, res) => {
let token = await getAzureToken()
// Prints out this in my browser: {}
res.send(token)
})https://stackoverflow.com/questions/68485833
复制相似问题