【发布时间】:2020-02-17 19:36:08
【问题描述】:
我正在尝试为我的应用制作报告功能
在前端我提出了一个 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 对象,如果包含一些嵌套对象,我需要做些什么吗?
谢谢
【问题讨论】:
标签: javascript node.js reactjs mongodb express