猫鼬人口不起作用

时间:2018-01-03 08:52:00

标签: node.js mongodb mongoose

我试图用评论填充帖子。随着作者的填充,我没有任何问题。我试图在没有作者的情况下填充评论,但它没有工作......

以下是评论模型:

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

0 个答案:

没有答案