【问题标题】:Unhandled promise rejection on repeat execution重复执行时未处理的承诺拒绝
【发布时间】:2018-07-20 15:12:45
【问题描述】:

这是我的代码:

app.post("/blogs/:id/comments", function (req, res) {
    blog.findById(req.params.id, function (err, finded) {
        if (err) {
            console.log(finded)
            console.log(err);
            console.log("finded data")
            res.redirect("/blogs");
        } else {
            Comment.create(req.body.comment, function (err, comment) {
                if (err) {
                    console.log(comment)
                    console.log(err);
                    console.log("my coomeent")


                } else {
                    finded.comments.push(comment);
                    finded.save();
                    res.redirect("/blogs/" + finded._id);
                }
            })
        }
    })
});

当我尝试第二次插入评论时,代码会出现以下错误:

.(node:6064) UnhandledPromiseRejectionWarning: 未处理的承诺 拒绝(拒绝 id:1):ValidationError:博客验证失败: cmets:强制转换为 [undefined] 失败 "[{"_id":"5a7dee043918a20128a1e39e","text":"嘿","author":"himansu","__v":0}]" 在路径“cmets”(节点:6064)[DEP0018] DeprecationWarning:未处理 承诺拒绝已被弃用。在未来,承诺拒绝 未处理的将终止 Node.js 进程 非零退出代码。

【问题讨论】:

    标签: javascript node.js mongodb mongoose-schema


    【解决方案1】:

    Himansu,我认为finded.comments.push(comment); 方法返回了一个承诺。所以你必须处理承诺。喜欢:

    finded.comments.push(comment)
    .then(result => {
        finded.save();
        res.redirect("/blogs/" + finded._id);
    })
    .catch(err => {
        console.log(err);
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-02
      • 2018-03-31
      • 2023-03-17
      • 2018-04-01
      • 2021-09-19
      • 1970-01-01
      相关资源
      最近更新 更多