这通常是由于忘记等待 Promise 被执行并返回结果。确保在请求结束之前使用 await 关键字或使用 .then() 处理 Promise 的结果。
例如,将以下代码段:
axios.get('https://example.com/api/data')
console.log('Data fetched!')
改为:
axios.get('https://example.com/api/data')
.then(response => console.log(response.data))
.catch(error => console.log(error))
或
async function fetchData() {
try {
const response = await axios.get('https://example.com/api/data')
console.log(response.data)
} catch (error) {
console.log(error)
}
}
fetchData()