javascript - 异步函数未捕获(承诺中)未定义错误
问题描述
我正在尝试在创建/更新条目之前进行一些验证,如下所示:
async save(){
return new Promise((resolve, reject)=>{
if(!this.isCampaignValid){
this.handleError()
reject()
}
else{
this.$store
.dispatch('updateCampaign')
.then((res)=>{
resolve()
this.showNotification(res.message, 'success')
})
.catch(error=>{
this.showNotification(error.message, 'error')
reject()
})
}
})
},
这isCampaignValid
是计算有效性的计算值。
如果活动无效,那么我会在控制台中收到如下错误:
Uncaught (in promise) undefined
该this.handleError()
功能也有效。如何处理这种承诺错误情况?
解决方案
以防万一 handleError() 抛出,尝试:
if (!this.isCampaignValid) {
try {
this.handleError()
} catch (e) {
console.error(e);
}
reject()
}
推荐阅读
- reactjs - 你如何使用 React-data-Grid 测试你的应用程序?
- javafx - 我正在尝试为 JavaFX 编写测试用例,如何开始?
- r - 如何获得分割的 R 包中所有回归线的截距的 95% CI 的限制?
- android - Firebase Analytics - 合并来自不同帐户的两个应用
- sql-server - 单个搜索引擎中多个名称的 Sql 查询
- azure - 多个具有相同公共 IP 地址的 azure 虚拟机
- python - 有没有一种方法可以使字典字典顺序忽略大小写?
- node.js - 下载后如何启动 Mongo Db。命令 ./mongod 不起作用
- udp - 使用 gStreamer 教程 3 将 UDP 流式传输到 Android 设备
- laravel - 如何在 Laravel Api 控制器上执行 Graphql Schema?