未处理的承诺拒绝重复执行

时间:2018-02-09 19:24:18

标签: javascript node.js mongodb mongoose-schema

这是我的代码:

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:未处理的承诺   rejection(拒绝id:1):ValidationError:博客验证失败:   注释:转换为[undefined]失败的值   " [{" _id":" 5a7dee043918a20128a1e39e""文本":"哎""作者" :" himansu"" __ v":0}]"   在路径"评论" (节点:6064)[DEP0018]弃用警告:未处理   承诺拒绝被拒绝。在未来,承诺拒绝   未处理的将使用a终止Node.js进程   非零退出代码。

1 个答案:

答案 0 :(得分:1)

Himansu,我认为方法TestScheduler会返回一个承诺。所以你必须处理这个承诺。像:

finded.comments.push(comment);