node.js - 如何解决未捕获(承诺)错误?
问题描述
我在检查->控制台中收到此错误
这是我的代码,出现错误 - ProductHelper.create(data)
if (this.props.data !== undefined) {
data.product_id = this.props.data.product_id;
data.is_active = is_active;
ProductHelper.update(data, this.props.data.product_id)
.then((data) => {
if (data.code == 200) {
alert("Product successfully updated!");
this.props.getProducts();
this.props.setVisibility(false);
} else {
throw "Incorrect seller id";
}
})
.catch((err) => {
throw err;
});
} else {
ProductHelper.create(data)
.then((data) => {
if (data.code == 200) {
alert("Product successfully created!");
this.props.getProducts();
this.props.setVisibility(false);
} else if (data.code == 401) {
alert("Your product limit has exceeded!");
} else {
throw "error";
}
})
.catch((err) => {
throw err;
});
}
} catch (err) {
alert("Error creating. Try Again Later!");
console.log(err);
}
}
我应该怎么办?
解决方案
我认为承诺失败并且无法找到错误块。您必须在承诺中添加一个 catch 语句。
promise
.then((res) => {})
.catch(err => console.log(err));
推荐阅读
- ios - 为什么 Swift ARC 不首先打破属性引用以防止保留循环?
- c++ - 如何在 c++20 约束算法中投影 std::tuple
- reactjs - useState 中的自定义钩子
- validation - excel文件的客户端验证在testcafe中失败
- angular - 如何在部署后使用 ctrl+click 在新选项卡中打开 Angular 应用程序
- c++ - FM10420(fm10k) DPDK RX 队列与 dmesg 上的 FUM_BAD_VF_QACCESS 一起变为空
- azure - 获取特定的 Azure 对象标记而不仅仅是资源组?
- continuous-integration - Checkmarx Getting Error Failed to authenticate: invalid_grant Failed to generate access token from refresh token failure error was:
- flask - Flask-JWT-Ext get_current_user(), get_jwt_identity() returns None despite having tokens
- javascript - Error implementing stripe payment in deployment (Heroku)