我试图用评论填充帖子。随着作者的填充,我没有任何问题。我试图在没有作者的情况下填充评论,但它没有工作......
以下是评论模型:
const mongoose = require('mongoose');
const schema = mongoose.Schema;
const User = require('./user');
commentSchema = new schema({
comment: String,
author: { type: schema.Types.ObjectId, ref: 'User' },
})
const Comment = module.exports = mongoose.model('Comment', commentSchema);
这是路线:
router.get('/posts/:id', (req, res) => {
Post.findById({ _id: req.params.id })
.populate('author')
.populate('comments')
.exec((err, post) => {
if (err) {
console.log(err);
} else {
res.json(post);
}
});
});