首页 文章

UnhandledPromiseRejectionWarning

提问于
浏览
-1

下面的参考代码块:

//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 ...”语句的ejs html模板中显示 . 但是,后续注释添加会在下面生成错误/警告:


(node:1400)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):ValidationError:营地验证失败:注释:转换为[undefined]失败,值为“[{”author“:{”id“:”5a800e7d0646ee0837c2787e“, “用户名”:“成功”},“_ id”:“5a84b148a953bc03b4f58180”,“文字”:“在木质环境中的好营地...... Yayy!我将在下周访问这个地方“,”__ v“:0}]”在路径“评论


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

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

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

下面是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

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

相关问题