mongoose .create给node.js“验证错误:验证失败:转换为[未定义]失败的值...”

时间:2018-01-10 11:46:44

标签: javascript arrays node.js express mongoose

我有一个应用程序,我想将注释推送到单个id页面(显示一个露营地)传递数组中的值。我用来表达和猫鼬。

错误

  

UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):   ValidationError:验证失败:转换为[undefined]失败   值...

这是要显示的页面模型:

模型/ campground.js

var mongoose = require("mongoose");

var campgroundSchema = new mongoose.Schema({
    name: String,
    image: String,
    description: String,
    comments: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: "Comment"
    }]
});

module.exports = mongoose.model("Campground", campgroundSchema);

campgroundSchema指的是评论模型:

模型/ comment.js

var mongoose = require("mongoose");

var commentSchema = new mongoose.Schema({
    text: String,
    author: String
});

module.exports = mongoose.model("Comment", commentSchema);

两者都用于创建评论路线,通过id找到一个露营地,然后创建一个从表单中检索数据的注释,试图将该注释推送到之前创建的露营地的comments数组中,并保存:

app.js

app.post("/campgrounds/:id/comments", function (req, res) {
    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(err);
                } else {
                    campground.comments.push(comment);
                    campground.save();
                    res.redirect("/campgrounds/" + campground._id);
                    console.log(req.body.comment);
                    }
            });

        }
    });
});

这是“添加新评论”表单的代码:

评论/ new.ejs

<div class="container">
    <div class="row">
        <h1 style="text-align: center;">Add a new Comment to <%= campground.name %></h1>
        <div style="width: 30%; margin:25px auto;">
            <form action="/campgrounds/<%= campground._id %>/comments" method="POST">
                <div class="form-group">
                    <input class="form-control" type="text" name="comment[text]" placeholder="text">
                </div>
                <div class="form-group">
                    <input class="form-control"  type="text" name="comment[author]" placeholder="author">
                </div>
                <div class="form-group">
                    <button class="btn btn-lg btn-primary btn-block">Submit!</button>
                </div>
            </form>
            <a href="/campgrounds">Go Back</a>
        </div>
    </div>
</div>

这是“露营地”页面:

露营地/ show.ejs

<div class="container">
    <div class="row">
        <div class="col-md-3 col-sm-6">
            <h1><%= campground.name %></h1>
            <div class="...">
                <img src="<%= campground.image %>" class="media-object">
                <p><%= campground.description %></p>
                <p><a class="btn btn-success" href="/campgrounds/<%= campground._id %>/comments/new">Add New Comment</a></p>
                <% campground.comments.forEach(function(comment) {%>
                    <p><strong><%= comment.text %></strong> - <%= comment.author %></p>
                <% })%>
                <a href="/campgrounds">Go Back</a>
            </div>
        </div>
    </div>
</div>

路线正确地重定向到“show campground”页面,但没有新的评论添加到循环中。我在控制台中有一个节点警告:

(node:1940) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): 
ValidationError: Campground validation failed: comments: Cast to [undefined] failed for value 
"[{
"_id":"5a55f5afd9bf5c0794ca7220",
"text":"previously existing comment",
"author":"first author",
"__v":0
}]" 
at path "comments"

请注意,记录的评论是现有评论,由于console.log(req.body.comment)示例,仅记录表单提交的评论:

{ text: 'lortem ipsum', author: 'nick beer' }

我该如何解决这个问题?

编辑:感谢JavaEvgen,我尝试了嵌套架构,应用程序无需更改其他行即可运行。但似乎我无法使用引用的模式。错误在哪里?

3 个答案:

答案 0 :(得分:1)

最简单的解决方案是使用嵌套模式同时保存你的campgound和评论:

var mongoose = require("mongoose");

var comment = new mongoose.Schema({
  text: String,
  author: String
});

var campgroundSchema = new mongoose.Schema({
  name: String,
  image: String,
  description: String,
  comments: [comment]
});

module.exports = mongoose.model("Campground", campgroundSchema);

在这种方法中,您可以像这样保存您的评论:

Campground.findById(req.params.id, function(err, campground) {
  if (err) throw err;
    campground.comments.push(req.body.comment);
      campgound.save(function(err, result){
        if(err) throw err;
            res.redirect("/campgrounds/" + campground._id);
    });
});

或者您也可以尝试避免使用Mongoose的.create函数并使用.save代替创建这样的注释:

var comment = new Comment(req.body.comment);
comment.save(your_callback_here);

除此之外,当我使用你最初尝试的方法时,我已经检查了我自己的解决方案,并且我发现了一些小差异。

首先,我还有一个从Comment模式到我的案例Movie的后向引用,所以我想你可以尝试将它添加到你的Comment模式中,如下所示:

var commentSchema = new mongoose.Schema({
    text: String,
    author: String,
    campground: { type: mongoose.Schema.Types.ObjectId, ref: 'Campground' }
});

在此之后,我认为你必须首先保存你的评论,这样才能从mongoDB获得一个_id,然后才能将这个id推送到campground对象。而你实际上是通过使用.create以正确的顺序进行的,但是由于没有任何作用,我认为你可以尝试这样的事情:

var comment = new Comment(req.body);
comment.campground = req.campground._id;

comment.save(function(err, comment){
    if(err) return next(err);

    comment.campground.comments.push(comment);
    comment.campground.save(function(err, campground){
        if(err) return next(err);
        res.redirect("/campgrounds/" + campground._id);
    });
});

答案 1 :(得分:0)

我使用引用ObjectId找到的唯一解决方案是以下代码:

app.post("/campgrounds/:id/comments", function (req, res) {
    Comment.create(req.body.comment, function (err, comment) {
        if (err) {
            console.log(err);
        } else {
            Campground.findOne({"_id": req.params.id})
            .populate("comments")
            .exec(function (err, campground) {
                if (err) {
                    console.log(err);
                    res.redirect("/campgrounds");
                } else {
                    campground.comments.push(comment);
                    campground.save();
                    res.redirect("/campgrounds/" + campground._id);
                    // console.log(req.body.comment);
                    // console.log(campground.comments);
                }
            });
        }
    });
});

希望有所帮助

答案 2 :(得分:0)

我们甚至需要将评论ID推送到露营地。

将此路线更改为如下所示:

app.post("/campgrounds/:id/comments", function (req, res) {
    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(err);
                } else {
                    campground.comments.push(comment);
                    campground.save();
                    res.redirect("/campgrounds/" + campground._id);
                    console.log(req.body.comment);
                    }
            });

        }
    });
});

到这条路线

app.post("/campgrounds/:id/comments", function(req,res){
   Campground.findById(req.params.id, function(err, campground) {
       if(err){
           console.log(err);
       } else {
           Comment.create(req.body.comment, function(err, comment){
              if(err){
                  console.log(err);
              } else {
                    campground.comments.push(comment);
                    campground.save();
                    campground.comments.push(comment._id);
                    campground.save();
                    res.redirect("/campgrounds/" + campground._id);
              }
           });
       }
   });
});