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