There are my mongoose schemas. I guess its ok. ( 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);
But im having trouble with post method. How can fix the code? When i post my json, its returning with empty "items".
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]
});
I edited my project like this. My problem is solved. Here is my schemas.
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);
This is my post request.
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
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.