1

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]  
    });
2
  • Please don't post images of code. It's easier for you to insert into the question and easier for us to debug. Commented Dec 10, 2020 at 22:49
  • Sorry, i edited. Commented Dec 11, 2020 at 10:04

1 Answer 1

1

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
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.