如何在Array Mongoose Schema中使用Array进行POST



有我的猫鼬模式。我想没关系(https://mongoosejs.com/docs/subdocs.html)

const mongoose = require("mongoose");
const itemSchema = mongoose.Schema({
"itemNo": {
"type": "String"
}
});
const mySchema = mongoose.Schema({
"items":[itemSchema],
"title": {
"type": "String"
},
"data": {
"type": "String"
},
"pic": {
"type": "String"
},
"star": {
"type": "Boolean"
},
"category": {
"type": "String"
},
"date": {
"type": "Date"
},
});
module.exports = mongoose.model('dashboards', mySchema);

但我在后处理方法上有问题。如何修复代码?当我发布json时,返回的是空的"项目";。

router.post("/", (req, res) => {
const post = new Model1({
title: req.body.title,
data: req.body.data,
pic: req.body.pic,
star: req.body.star,
category: req.body.category,
items: [req.body.items]  
});

我这样编辑我的项目。我的问题解决了。这是我的模式。

const mongoose = require("mongoose");
const itemSchema = mongoose.Schema({
"itemNo": {
"type": "String"
},
});
const mySchema = mongoose.Schema({
"title": {
"type": "String"
},
"data": {
"type": "String"
},
"pic": {
"type": "Date"
},
"star": {
"type": "Boolean"
},
"category": {
"type": "String"
},
"items": [itemSchema]
});
module.exports = mongoose.model('dashboards', mySchema);

这是我的帖子请求。

router.post("/", (req, res) => {
const post = new Model1({
title: req.body.title,
data: req.body.data,
pic: req.body.pic,
star: req.body.star,
category: req.body.category,
items: req.body.items
});

最新更新