【问题标题】:mongoose callback hell to promise猫鼬回调地狱承诺
【发布时间】:2020-12-24 11:08:14
【问题描述】:

我想将此回调切换为 Promise,但由于模型中的关系而遇到困难

router.post("/", middleware.isLoggedIn, (req, res) => {
  Campground.findById(req.params.id, (err, campground) => {
    if (err) {
      console.log(err);
      redirect("/campgrounds");
    } else {
      Comment.create(req.body.comment, (err, comment) => {
        if (err) {
          console.log(err);
        } else {
          comment.author.id = req.user._id;
          comment.author.username = req.user.username;
          comment.save();
          campground.comments.push(comment);
          campground.save();
          res.redirect("/campgrounds/" + campground._id);
          console.log("comment created");
        }
      });
    }
  });
});

尝试做我自己,但它出错了“无法读取属性 'cmets' of null”

router.post("/", (req, res) => {
  const text = req.body.text;
  const newComment = new Comment({ text });
  Campground.findById(req.params.id)
    .then((foundCampground) => {
      newComment
        .save()
        .then((comment) => {
          comment.author.id = req.user._id;
          comment.author.username = req.user.username;
          comment.save();
          foundCampground.comments.push(comment);
          foundCampground.save();
        })
        .then(() => res.json("Comment added!"));
    })
    .catch((err) => {
      res.json(`Error ${err}`);
    });
});

TIA

【问题讨论】:

    标签: javascript node.js mongoose promise es6-promise


    【解决方案1】:

    您的数据库中似乎没有foundCampground 的记录。总是在推送到数组之前测试是否有从数据库返回的记录。 findById 如果没有特定查询的记录,则返回 null

    router.post("/", (req, res) => {
      const text = req.body.text;
      const newComment = new Comment({ text });
      Campground.findById(req.params.id)
        .then((foundCampground) => {
          newComment
            .save()
            .then((comment) => {
              comment.author.id = req.user._id;
              comment.author.username = req.user.username;
              comment.save();
              if(foundCampground !== null){ //Added these line
              foundCampground.comments.push(comment);
              }
              foundCampground.save();
            })
            .then(() => res.json("Comment added!"));
        })
        .catch((err) => {
          res.json(`Error ${err}`);
        });
    });
    

    【讨论】:

    • 不过,您可能需要在保存评论之前检查一下
    【解决方案2】:

    回答

    猫鼬中的查询不是承诺。为了将您的查询更改为 Promise,您必须使用 .exec() 函数。

    示例

    router.post("/", (req, res) => {
      const text = req.body.text;
      const newComment = new Comment({ text });
      Campground.findById(req.params.id)
        .exec() // This line makes the trick
        .then((foundCampground) => {
          newComment
            .save()
            .then((comment) => {
              comment.author.id = req.user._id;
              comment.author.username = req.user.username;
              comment.save();
              foundCampground.comments.push(comment);
              foundCampground.save();
            })
            .then(() => res.json("Comment added!"));
        })
        .catch((err) => {
          res.json(`Error ${err}`);
        });
    });
    

    参考

    Mongoose: Queries are not promises

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-15
      • 1970-01-01
      • 2019-09-18
      • 2015-05-12
      • 2019-02-13
      • 2016-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多