首页 > 解决方案 > 异步函数未捕获(承诺中)未定义错误

问题描述

我正在尝试在创建/更新条目之前进行一些验证,如下所示:

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()功能也有效。如何处理这种承诺错误情况?

标签: javascriptvue.js

解决方案


以防万一 handleError() 抛出,尝试:

if (!this.isCampaignValid) {
  try {
    this.handleError()
  } catch (e) {
    console.error(e);
  }
  reject()
}

推荐阅读