UnhandledPromiseRejectionWarning

时间:2018-02-17 10:45:06

标签: node.js mongodb express ejs

下面的参考代码块:

//create comment route: post new comment to database
router.post("/campgrounds/:id/comments", isLoggedIn, function (req,res){
  //lookup campground using ID
  Campground.findById(req.params.id, function(err,campground){
    if(err){
      console.log(err);
      res.redirect("/campgrounds");
    } else {
      Comment.create(req.body.comment, function(err, comment){
        if(err){
          console.log("Error Occured");
          console.log(err);
        } else {
          //add username and id to comment
          console.log("New Comment User Name will be " + req.user.username);
          comment.author.id = req.user._id;
          comment.author.username = req.user.username;
          //save comment
          comment.save();
          campground.comments.push(comment);
          campground.save();
          console.log(comment);
          res.redirect("/campgrounds/" + campground._id);
        }
      })
    }
  });

});

我意识到,如果我清空了数据库,创建了一个新的露营地,将第一条评论添加到该露营地总是成功的,并显示在" res.redirect ...&#34的ejs html模板中;声明。但是,后续添加注释会在下面生成错误/警告:

  

(node:1400)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):ValidationError:营地验证失败:评论:投放到[未定义]失败的值" [{" author" :{" ID":" 5a800e7d0646ee0837c2787e""用户名":"成功"}" _id":&# 34; 5a84b148a953bc03b4f58180","文字":"在木质环境中的好营地...... Yayy!我将在下周访问这个地方",#34; __ v":0}]"在路径"评论

尽管上面的警告/错误代码仍然执行到该行;

res.redirect("/campgrounds/" + campground._id);

然而,/ campgrounds /"的ejs html模板+ campground._id包含一个例程,用于遍历特定露营地的评论并显示所有这些,只显示第一条评论,即使对露营地有多条评论。

下面是ejs模板:

<% include ../partials/header %>

<div class=container>
  <div class = "row">
      <div class="col-md-3">
        <p class="lead">YelpCamp</p>

        <div class = "list-group">
            <li class = "list-group-item active">Info 1</li>
            <li class = "list-group-item">Info 2</li>
            <li class = "list-group-item">Info 3</li>
        </div>
      </div>

      <div class="col-md-9">
        <div class="thumbnail border">
            <img class="img-responsive" src=" <%= campground.image %> " alt="">
            <div class="caption-full">
                <h4 class="float-right">$9.00/night</h4>
                <h4><a href="/"><%=campground.name%></a></h4>
                <p class="text-justify">
                  <%= campground.description %>
                </p> 

            </div>
        </div>
        <p></p>
        <div class="card bg-light">
            <div class ="card card-header">
              <div class = "text-right">
                  <a class="btn btn-success" href="/campgrounds/<%= campground._id %>/comments/new">Add new Comment</a>
              </div>
              <p></p>
            </div>
                <% campground.comments.forEach(function(comment){ %>
                <div class="row"> 
                  <div class="col-md-12">
                      <strong> <%= comment.author.username %> </strong> 
                      <span class="float-right">10 days ago</span>
                      <p> 
                        <%= comment.text %> 
                      </p>

                  </div>
                  <hr>
                </div>
               <% }); %>

        </div>
      </div>
  </div>
</div>


<% include ../partials/footer %>

1 个答案:

答案 0 :(得分:0)

从5.0.1更新mongoose版本到5.0.6清除了警告。