我正在通过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);
}
}
答案 0 :(得分:3)
await
关键字正在等待一个承诺(这意味着它在内部处理then
),但是不会返回承诺。而是await
返回承诺的结果。
因此,执行所需操作的正确方法是:
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
关键字返回一个Promise。因此,您应该这样调用函数:
sendUserData().then(console.log('done'));