猫鼬停止了填充

时间:2018-01-14 08:27:55

标签: node.js mongodb express mongoose

正如标题所说,猫鼬停止填写帖子的评论。它与作者合作,昨天起作用。我没有用post post route改变任何东西。真的很混乱,填充方法有没有可能导致这种情况的限制因为注释数组有很多对象id虽然注释集合中没有注释。

这是后路线:

    router.get('/posts/:id', (req, res) => {
    Post.findById({ _id: req.params.id })
        .populate('author')
        .populate({
            path: 'comments', populate: {
                path: 'author'
            }
        })
        .exec((err, post) => {
            if (err) throw err;
            else {
                res.json(post);
            }
        });
});

此外,还有旧评论,但新评论不存在,尽管它们已保存在评论集中。

编辑:帖子架构:

const mongoose = require('mongoose');
const schema = mongoose.Schema;

const User = require('./user');
const Comment = require('./comment');

postSchema = new schema({
    title: String,
    content: String,
    author: { type: schema.Types.ObjectId, ref: 'User' },
    postImageUrl: String,
    comments: [{ type: schema.Types.ObjectId, ref: 'Comment' }]
})

const Post = module.exports = mongoose.model('post', postSchema, 'posts');

评论架构:

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.post('/posts/:id/comments', passport.authenticate('jwt'), (req, res) => {
    var newComment = new Comment({
        comment: req.body.comment,
        author: req.user._id
    });
    newComment.save((err, comment) => {
        if (err) {
            res.json({ success: false, message: 'Error' + err });
        } else {
            res.json({ success: true, message: 'Success', comment });
        }
    });
});

已解决:我应该在帖子评论数组中取消移位或推送创建的评论..

0 个答案:

没有答案