【问题标题】:mongoose .create give node.js "ValidationError: validation failed: Cast to [undefined] failed for value..."mongoose .create 给 node.js “ValidationError:验证失败:转换为 [undefined] 值失败......”
【发布时间】:2018-06-19 14:03:42
【问题描述】:

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

错误

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

这是要显示的页面模型:

models/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 模型:

models/comment.js

var mongoose = require("mongoose");

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

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

两者都用于创建评论路线,通过 id 找到露营地,然后创建评论从表单中检索数据,尝试将该评论推送到先前建立的露营地的 cmets 数组中,然后保存:

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);
                    }
            });

        }
    });
});

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

cmets/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 campground”页面:

campgrounds/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,我尝试了嵌套模式,并且该应用程序可以在不更改其他行的情况下运行。但似乎我无法使用引用的模式。错误在哪里?

【问题讨论】:

  • 尝试将您的“营地”作为第三个参数传递给 Comment.create 函数。我认为那里只是未定义
  • 露营地由Campground.findById()经过。如果我console.log(campground); 我的露营地记录了新评论,但没有第一条评论。每次我添加评论都是一样的,登录控制台新评论(只有一个),但显示旧评论
  • 不是campground.comments.push(comment);,而是Campground.comments.push(comment._id);
  • 我尝试了您的代码,但由于错误:Cannot read property 'push' of undefined 而无法正常工作。 Campground 是我要更新的特定 campground 的型号名称。 comment 是要推入数组的对象,我不想将_id 而是将对象的所有字段推入campground.comments 数组
  • 另一种解决方案是不对 cme​​ts 使用单独的模式。您可以使用“嵌套”模式。这样会容易得多。

标签: javascript arrays node.js express mongoose


【解决方案1】:

最简单的解决方案是使用嵌套模式来保存您的 campgound 和 cmets,如下所示:

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);

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

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 来创建您的 cmets,如下所示:

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

除此之外,当我使用您最初尝试的方法时,我已经检查了自己的解决方案,我注意到了一些小的差异。

首先,在我的情况下,我也有一个从评论模式到电影的向后引用,所以我认为您可以尝试将它也添加到您的评论模式中,如下所示:

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);
    });
});

【讨论】:

  • 谢谢。使用嵌套代码有效,我很想知道如何在猫鼬模型中配置引用以仅推送 Id 而不是整个子数组
  • 我很高兴听到它有效。如果它对您有用,如果您将我的答案标记为正确的答案,那也很棒。有关您的问题的更多信息,您可以阅读 mongoose here 的官方文档。我很确定你错过了一些非常小的东西,因为在我看来你的代码看起来是正确的
  • 谢谢,我会尝试一些代码,也许会打开一个关于猫鼬参考与嵌入模式的新问题。
【解决方案2】:

我发现使用引用 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);
                }
            });
        }
    });
});

希望对你有帮助

【讨论】:

    【解决方案3】:

    我们甚至需要将评论 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);
                  }
               });
           }
       });
    });
    

    【讨论】:

      猜你喜欢
      • 2018-06-30
      • 2019-05-04
      • 2020-08-20
      • 2015-09-30
      • 2020-07-22
      • 2016-02-14
      • 2013-03-24
      • 2021-09-06
      • 2018-01-06
      相关资源
      最近更新 更多