我正在为item.comments列表添加评论 . 我需要在我在响应中输出之前获取comment.created_by用户数据 . 我该怎么做?
Item.findById(req.param('itemid'), function(err, item){
var comment = item.comments.create({
body: req.body.body
, created_by: logged_in_user
});
item.comments.push(comment);
item.save(function(err, item){
res.json({
status: 'success',
message: "You have commented on this item",
//how do i populate comment.created_by here???
comment: item.comments.id(comment._id)
});
}); //end item.save
}); //end item.find
我需要在res.json输出中填充comment.created_by字段:
comment: item.comments.id(comment._id)
comment.created_by是我的mongoose CommentSchema中的用户引用 . 它目前只给我一个用户ID,我需要填充所有用户数据,密码和盐字段除外 .
以下是人们提出的架构:
var CommentSchema = new Schema({
body : { type: String, required: true }
, created_by : { type: Schema.ObjectId, ref: 'User', index: true }
, created_at : { type: Date }
, updated_at : { type: Date }
});
var ItemSchema = new Schema({
name : { type: String, required: true, trim: true }
, created_by : { type: Schema.ObjectId, ref: 'User', index: true }
, comments : [CommentSchema]
});
4 回答
为了填充引用的子文档,您需要显式定义ID引用的文档集合(如
created_by: { type: Schema.Types.ObjectId, ref: 'User' }
) .鉴于此引用已定义且您的架构也已正确定义,您现在可以像往常一样调用
populate
(例如populate('comments.created_by')
)概念证明代码:
最后请注意
populate
仅适用于查询,因此您需要先将项目传递给查询然后调用它:这可能是在编写原始答案后发生了变化,但看起来您现在可以使用Models populate函数来执行此操作,而无需执行额外的findOne . 见:http://mongoosejs.com/docs/api.html#model_Model.populate . 你想在保存处理程序中使用它,就像findOne一样 .
@ user1417684和@ chris-foster是对的!
从工作代码中摘录(没有错误处理):
我遇到了同样的问题,但经过几个小时的努力,我找到了解决方案 . 它可以不使用任何外部插件:)