猫鼬协会无法找到并更新

时间:2019-07-19 18:36:44

标签: node.js mongodb mongoose

我正在尝试通过邮递员添加一个新的评论记录:

{
    "content": "partent",
    "user_id": "42beec50-76de-4277-b435-f9421dffb7c3",
    "user_name": "Schoenbl",
    "_report": "5d30c66323226e0ddc684b50",
    "parent_comment_id": null
}

该记录已添加,但未与_report关联。

这是我添加评论的方式:

    comment.save((err, doc) => {
        if (err) return res.send(err)

        console.log('in router - card id', req.body._report);
        console.log('doc id', doc);
        console.log('req.body._report', req.body._report);

        Report.findOneAndUpdate({ _id: req.body._report },
            { $push: { comment: doc._id } },
            { new: true , useFindAndModify: false }
        )

        Comment.findOneAndUpdate({ _id: req.body.parent_comment_id },
            { $push: { reply: doc._id } },
            { new: true , useFindAndModify: false },
            (err, post) => {
                if (err) return res.send(err);

                res.json({doc})
            }
        )
    })
})

这是我的评论模型:

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

let Comment = new Schema({
    content: {
        type: String
    },
    user_id: {
        type: String
    },
    user_name: {
        type: String
    },
    _report: {
        type: String,
        ref: 'Report'
    },
    parent_comment_id: {
        type: String,
        ref: 'Comment'
    },
    reply: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Comment',
    }]
}, { timestamps: true })

module.exports = mongoose.model('Comment', Comment)

这是我的报告模型:

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

let Report = new Schema({
    title: {
        type: String
    },
    summary: {
        type: String
    },
    analysis: {
        type: String
    },
    source_title: {
        type: String
    },
    source_link: {
        type: String
    },
    author: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
    },
    like: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Like'
    }],
    comment: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Comment'
    }],
    player: {
        type: mongoose.Schema.Types.ObjectId,
        required: true,
        ref: 'Player'
    }
}, { timestamps: true })

module.exports = mongoose.model('Report', Report)

当我查看相关报告的输出时,它显示了一个空数组。

0 个答案:

没有答案
相关问题