Then 不是 axios async/await post 请求上的函数
Then is not a function on axios async/await post request
我正在通过 POST
请求注册用户。
为此,我将 axios 与 async/await 结合使用!但是,我收到 register.then is not a function
错误。请帮帮我。
async sendUserData() {
try {
const register = await axios.post('/register', {
email: this.register.email.trim(),
password: this.register.password.trim(),
});
register.then(
response => {
console.log(response);
}
);
} catch (e) {
console.log(e);
}
}
await
关键字等待承诺(这意味着它在内部处理 then
)但它不 return 承诺。相反 await
return 是承诺的结果。
因此,做你想做的正确方法是:
async sendUserData() {
try {
const response = await axios.post('/register', {
email: this.register.email.trim(),
password: this.register.password.trim(),
});
console.log(response);
} catch (e) {
console.log(e);
}
}
但是,async
关键字 return 是一个承诺。所以你应该这样调用你的函数:
sendUserData().then(console.log('done'));
我正在通过 POST
请求注册用户。
为此,我将 axios 与 async/await 结合使用!但是,我收到 register.then is not a function
错误。请帮帮我。
async sendUserData() {
try {
const register = await axios.post('/register', {
email: this.register.email.trim(),
password: this.register.password.trim(),
});
register.then(
response => {
console.log(response);
}
);
} catch (e) {
console.log(e);
}
}
await
关键字等待承诺(这意味着它在内部处理 then
)但它不 return 承诺。相反 await
return 是承诺的结果。
因此,做你想做的正确方法是:
async sendUserData() {
try {
const response = await axios.post('/register', {
email: this.register.email.trim(),
password: this.register.password.trim(),
});
console.log(response);
} catch (e) {
console.log(e);
}
}
但是,async
关键字 return 是一个承诺。所以你应该这样调用你的函数:
sendUserData().then(console.log('done'));