【发布时间】:2016-01-01 07:03:52
【问题描述】:
我正在尝试为帖子保存 cmets。当我从客户端发布评论时,评论应该与我从帖子页面收集的帖子的 ObjectId 一起保存 - req.body.objectId。我尝试了下面的方法,但它只会给我验证错误。
型号
var Comment = db.model('Comment', {
postId: {type: db.Schema.Types.ObjectId, ref: 'Post'},
contents: {type: String, required: true}
}
发布
router.post('/api/comment', function(req, res, next){
var ObjectId = db.Types.ObjectId;
var comment = new Comment({
postId: new ObjectId(req.body.objectId),
contents: 'contents'
}
我怎样才能做到这一点?这是实现此类功能的正确方法吗?提前谢谢你。
【问题讨论】: