为什么即使已成功将注释保存到数据库,也未显示注释?

问题描述

当我执行console.log(comments)时,它会打印注释ID;当我执行console.log(post.comments.text)时,它会显示undefined。但是,注释已成功保存在数据库中,并且

// COMMENT FUNCTION

router.post("/:id/comments",middleware.isLoggedIn,function(req,res) {
  //look up for post using id
  Post.findById(req.params.id,function(err,post) {
    if (err) {
      console.log(err);
      req.flash("error","something went wrong")
      res.redirect("/posts");
    } else {
      //Create comment
      req.body.comment.body = req.sanitize(req.body.comment.body);

      Comment.create(req.body.comment,comment) {
        if (err) {
          console.log(err);
          req.flash("error","OOPS! something went wronng")
        } else {
          //add username and id to comment
          comment.author.id = req.user._id;
          comment.author.username = req.user.username;
          comment.save();
          post.comments.push(comment);
          post.save();
          console.log(post.comments.text);

          req.flash("success","Comment added successfully");

          res.redirect("/posts/" + post._id);
          //console.log("comment is saved");
        }
      });
    }
  });
});

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)