【问题标题】:how to solve Uncaught (in promise) error?如何解决未捕获(承诺)错误?
【发布时间】:2021-05-24 19:55:34
【问题描述】:

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

enter image description here

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

我该怎么办?

【问题讨论】:

  • 请发布一些代码,以便我们了解发生了什么。
  • 可以分享一下控制器吗?
  • @EduardHasanaj 这段代码是否足够,或者我应该发布整个代码。我不知道错误在哪里。所以我不发布代码
  • @Keerthika 你在 catch 中抛出了一个错误。如果外部函数未包含在 try catch 块中,则不应在此处抛出错误。

标签: node.js reactjs


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-19
    • 2017-11-18
    • 2021-11-28
    • 2018-05-12
    • 2019-08-20
    相关资源
    最近更新 更多