gpt4 book ai didi

javascript - mongoose .create 给 node.js "ValidationError: validation failed: Cast to [undefined] failed for value..."

转载 作者:行者123 更新时间:2023-11-30 20:51:02 26 4
gpt4 key购买 nike

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

错误

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ValidationError: validation failed: Cast to [undefined] failed for value...

这是要显示的页面模型:

模型/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 找到一个露营地,然后创建一个评论从表单中检索数据,试图将该评论推送到先前创建的露营地的评论数组中,并保存:

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

}
});
});

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

comments/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 警告:

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

最佳答案

最简单的解决方案是使用嵌套架构来保存您的营地和评论,如下所示:

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

关于javascript - mongoose .create 给 node.js "ValidationError: validation failed: Cast to [undefined] failed for value...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48186787/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com