【发布时间】: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数组 -
另一种解决方案是不对 cmets 使用单独的模式。您可以使用“嵌套”模式。这样会容易得多。
标签: javascript arrays node.js express mongoose