首页 > 解决方案 > 如何解决未捕获(承诺)错误?

问题描述

我在检查->控制台中收到此错误

在此处输入图像描述

这是我的代码,出现错误 - 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);
    }
}

我应该怎么办?

标签: node.jsreactjs

解决方案


我认为承诺失败并且无法找到错误块。您必须在承诺中添加一个 catch 语句。

promise
.then((res) => {})
.catch(err => console.log(err));

推荐阅读