Javascript 如何更新嵌套的mongo对象

Javascript 如何更新嵌套的mongo对象,javascript,node.js,reactjs,mongodb,express,Javascript,Node.js,Reactjs,Mongodb,Express,我正在尝试为我的应用程序创建报告功能 在前端,我提出一个put请求: .put(`http://localhost:3000/api/posts/report`, { params: { id: mongoId, reportInfo: { reported: true,

我正在尝试为我的应用程序创建报告功能

在前端,我提出一个put请求:

.put(`http://localhost:3000/api/posts/report`, {
                        params: {
                            id: mongoId,
                            reportInfo: {
                                reported: true,
                                reportingUser: id
                            }
                        }
                    })

到该后端路由

router.put('/report', (req, res, next) => {
    postModel.findOneAndUpdate(
        { _id: mongoose.Types.ObjectId(req.query.id) },
        req.query,
        { new: true, useFindAndModify: false },
        (error, returnedDocuments) => {
            if (error) return next(error);
            res.json(returnedDocuments);
        }
    );
});
对于这个模型

const postSchema = new mongoose.Schema(
    {
        title: { type: String },
        description: { type: String },
        image: { type: String },
        price: { type: String },
        location: { type: String },
        image: { type: Array },
        author: {
            type: String,
            ref: 'User'
        },
        reportInfo: {
            reported:{
                type: Boolean,
                default: false
            },

            reportingUser:{
            type: String
            }

        }
    },
    {
        timestamps: true
    }
);
知道它为什么不更新reportInfo对象吗?如果包含一些嵌套对象,我需要做些什么吗


谢谢

您的代码试图替换entires MongoDB文档。尝试使用而不是直接传递
req.query

{ $set: { reportInfo: req.query.reportInfo } }

我还将检查是否应该有
req.query
req.body
,因此只需打印该值以确保其正确反序列化。

您的代码尝试替换entires MongoDB文档。尝试使用而不是直接传递
req.query

{ $set: { reportInfo: req.query.reportInfo } }

我还将检查是否应该有
req.query
req.body
,因此只需打印该值以确保其正确反序列化。

是的,它将显示为未定义。。。req.query是否仅适用于某些类型的请求?我使用query传递axios delete对象的数据,因此我不确定为什么put req不适用。@jcx3x通常应将其作为HTTP正文传递,并在nodejsyeah中的
req.body
下可见,其显示为未定义。。。req.query是否仅适用于某些类型的请求?我使用query传递axios delete对象的数据,因此我不确定为什么put req不适用。@jcx3x通常应作为HTTP正文传递,并在nodejs中的
req.body
下可见