【发布时间】:2021-05-30 20:38:47
【问题描述】:
我有一个 axios post call:
export async function createFriend ({
let myData = new FormData()
myData.append('name', name)
myData.append('age', age)
const response = await axios.post('/myApi/friends', myData, {
headers: {
'Content-Type': 'multipart/form-data'
}
})
return response.data.id
}
我想将此迁移到 fetch 调用:
export async function createFriend ({
let myData = new FormData()
myData.append('name', name)
myData.append('age', age)
const response = await fetch('/myApi/friends', {
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data'
},
data: myData
})
return response.data.id
}
但是我收到以下错误: v-on 处理程序中的错误(Promise/async):“TypeError:无法读取未定义的属性'id'。但这在 axios 调用中没有发生。我做错了什么?
【问题讨论】:
-
您假设
fetch响应对象的形状与axios响应对象的形状相同。 -
您能协助处理来自 fetch 调用的正确响应对象吗?
标签: javascript vue.js vuejs2 axios fetch