当我尝试这样做时,其余的数据都被发布了,但是数组仍然为空
我的Schma看起来像这样
const mongoose = require('mongoose');
const DocumentSchema = mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'users',
},
dateCreated: String,
title: String,
// ID of the users that created the document
creatorId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'users',
},
comment: [{ title: String, detail: String }]
});
module.exports = mongoose.model('myDocument', DocumentSchema);
我在邮递员中的Post脚本看起来像这样
{
"label":"Got More data",
"linkUrl":"www.google.com",
"title":"test 14",
"comment":[
{
"title":"hello",
"detail":"some detail"
}]
}
当我获得更新的数据时,它看起来像这样
{
"_id": "5eb91c0e35eecb369696b9c5",
"title": "test 14",
"creatorId": "5d8856e5903a260588b77c30",
"user": "5d8856e5903a260588b77c30",
"dateCreated": "Mon May 11 2020 10:34:06 GMT+0100 (British Summer Time)",
"comment": [],
"__v": 0
}
我尝试过的其他事情
我已经尝试根据堆栈溢出How to define object in array in Mongoose schema correctly with 2d geo index
上的这篇文章更改架构定义对此
comment : [{
title : String,
detail : String
}]
这种方法也是
comment : {
"title":"A title",
"detail": "some random comment"
}
但是我仍然得到一个空白数组
答案 0 :(得分:0)
问题出在react api设置中,我没有将文件添加到以下api代码中是包含“ comment”字段的新代码(在imageUrl下)。现在可以使用
router.post(
'/',
[
auth,
[
check('title', ' myDocument Title is required')
.not()
.isEmpty()
]
],
async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
const { title, summary, imageURL, template, comment } = req.body;
try {
const newMyDocument = new MyDocument({
title,
summary,
imageURL,
comment,
template,
creatorId: req.user.id,
user: req.user.id,
columnOrder:["col1","col2","col3"],
columns:[
{col_1 :
{
id:"col_1",
title:"facts",
taskIds:["test1","test2"]
}
},
{col_2 :
{
id:"col_2",
title:"Pros",
taskIds:["test1","test2"]
}
},
{col_3 :
{
id:"col_3",
title:"Cons",
taskIds:["test1","test2"]
}
}
],
dateCreated:new Date()
});
const myDocument = await newMyDocument.save();
///this is the request to othe server
res.json(myDocument);
} catch (err) {
res.status(500).send('Server Error 76');
}
}
);