【发布时间】:2017-02-02 17:21:05
【问题描述】:
我已经定义了一个 PostSchema by mongoose:
var PostSchema = new mongoose.Schema({
comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }],
editor: { post: { type: mongoose.Schema.Types.ObjectId, ref: 'Post' }, codes: [{ type:mongoose.Schema.Types.ObjectId, ref: 'Code' }] }
});
因为这段代码,我可以使用http://localhost:3000/#/posts/5879fc91b7a56002237f30f5 来显示所有的cmets:
router.get('/posts/:post', function (req, res, next) {
req.post.populate('comments', function (err, post) {
if (err) { return next(err); }
res.json(post);
});
});
现在,我想用http://localhost:3000/#/posts/5879fc91b7a56002237f30f5/editor来获取editor的所有内容,包括codes里面的内容。但是下面的代码打印出["5892bbc68abcc926f7f1dd93","5892bbdf8abcc926f7f1dd94","5892bc178abcc926f7f1dd95","5892bc1b8abcc926f7f1dd96"]:
router.get('/posts/:post/editor', function (req, res, next) {
req.post.editor.populate('codes', function (err, editor) {
if (err) { return next(err); }
console.log(JSON.stringify(editor.codes));
res.json(editor);
})
});
有谁知道如何正确填充codes?
【问题讨论】:
标签: mongoose mongoose-populate