javascript - then 不是 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
),但它不返回一个承诺。而是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
关键字返回一个承诺。所以你应该这样调用你的函数:
sendUserData().then(console.log('done'));
推荐阅读
- algorithm - 点数最多的路段算法分析
- javascript - React 组件是否会深入比较 props 以检查是否需要重新渲染?
- php - Laravel 从文件中读取数组的最佳实践
- web2py - web2oy中多个字段名一起表示数据库记录
- mysql - 如果数据为空,则通过比较所有列来更新列
- c# - 使用 c# 的 WebSocket 连接问题
- rdf - 如何在 OWL 2 中的规则和属性链之间做出决定?
- javascript - Firebase 身份验证 id 令牌在到期时间之前到期
- java - SonarQube:无法从服务器获取引导索引:url [http://localhost:9000/batch/index] 返回的状态无效:[403]
- git - git 在(强制)更新 PR 后获取更改的更改