我无法在博客页面的评论部分发表评论:
app.post("/campgrounds/:id/comments", function(req,res){
//lookup for the campground 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(err);
}else{
campground.comments.push(comment);
campground.save();
res.redirect("/campgrounds/" + campground._id);
}
});
}
});
});
我得到的错误是
> (node:9300) UnhandledPromiseRejectionWarning: Unhandled promise
> rejection (rejection id: 1): ValidationError: comments: Cast to
> [undefined] failed for value
> "[{"_id":"5a6f2d7a1463e52454e367f4","text":"I wish I witness this
> serenity with my naked eyes","author":"shaykoo","__v":0}]" at path
> "comments" { comments: [ { _id: 5a6f2d7a1463e52454e367f4,
> text: 'I wish I witness this serenity with my naked eyes',
> author: 'shaykoo',
> __v: 0 } ], _id: 5a6f2d791463e52454e367f0, name: 'Indian camps', image:
> 'https://farm9.staticflickr.com/8225/8524305204_43934a319d.jpg',
> desc: 'Here is the romanian campground', __v: 1 }
露营地架构
var mongoose= require("mongoose");
var campgroundSchema = new mongoose.Schema({
name: String,
image: String,
desc:String,
comments: [
{
type :mongoose.Schema.Types.ObjectId ,
ref:"Comment" //"Comment is the Model name"
}
]
});
module.exports = mongoose.model( “营地”,campgroundSchema);
评论架构
var mongoose= require("mongoose");
var commentSchema = new mongoose.Schema({
text: String,
author: String
});
module.exports = mongoose.model( “注释”,commentSchema); //这里评论是模型的单数名称