1

I have this complex category schema which contains an array of subcategories and each subcategory has an array of books..

Could someone help me make a proper POST request for this category schema where I want the request body to look like that schema to contain a category nested in it an array of subcategories nested in each subcategory an array of books with Mongoose and NodeJS. This is the schema :-

{
    "categoryName": "engineering",
    "subcategories": [
        {
            "subcategoryName": "chemical enginnering",
            "books": [
                {
                    "bookName": "",
                    "author": "",
                    "ratings": [3, 4, 5, 3],
                    "feedbcks": ["awesome book", "very amazing book"],
                    "price": 222
                }
            ]
        },
        {
            "subcategoryName": "mechanical engineering",
            "books": [
                {
                    "bookName": "",
                    "author": "",
                    "ratings": [3, 4, 5, 3],
                    "feedbcks": ["awesome book", "very amazing book"],
                    "price": 222
                }
            ]
        }
    ]
}

I tried this code but doesn't make it

const category = new Category({
    categoryName: req.body.categoryName,
    subcategories: [
      new SubCategory({
        subCategoryName: req.body.subCategoryName,
        books: [
          new Book({
            bookName: req.body.bookName,
            price: req.body.price,
            ratings: req.body.ratings,
            feedbacks: req.body.feedbacks,
            author: req.body.author
          })
        ]
      })
    ]
  });

  try {
    const savedCategory = await category.save();
    res.send(savedCategory);
  } catch (error) {
    res.send(error.message);
  }

1 Answer 1

1

I believe that your schema could look like this (subdocuments):

const Category = new Schema({
    categoryName: String,
    subcategories: [{
        subcategoryName: String,
        books: [{
            bookName: String,
            author: String,
            ratings: Array,
            feedbacks: Array,
            price: Number
        }]
    }]
})

or

const Book = new Schema({
    bookName: String,
    author: String,
    ratings: Array,
    feedbacks: Array,
    price: Number
})

const Subcategory = new Schema({
    subcategoryName: String,
    books: [Book]  
})

const Category = new Schema({
    categoryName: String,
    subcategories: [Subcategory]
})

To save it, I think you could just pass the entire data object into your Model:

const category = {
    "categoryName": "engineering",
    "subcategories": [
        {
            "subcategoryName": "chemical enginnering",
            "books": [
                {
                    "bookName": "",
                    "author": "",
                    "ratings": [3, 4, 5, 3],
                    "feedbacks": ["awesome book", "very amazing book"],
                    "price": 222
                }
            ]
        },
        {
            "subcategoryName": "mechanical engineering",
            "books": [
                {
                    "bookName": "",
                    "author": "",
                    "ratings": [3, 4, 5, 3],
                    "feedbacks": ["awesome book", "very amazing book"],
                    "price": 222
                }
            ]
        }
    ]
}


const newCategory = new Category(category);
try {
    const savedCategory = await newCategory.save();
    res.send(savedCategory);
} catch (error) {
    res.send(error.message);
}

Of course, it means you will need to create this kind of object first in order to pass it, I used your data as an example. You can sort either in your front-end or back-end, whatever makes more sense to you.


Alternatively, you could create the Category first:

const newCategory = new Category({ categoryName: req.body.categoryName })

then add remaining elements (with arrays you could use loop to push elements dynamically):

const newCategory.subcategories = [{
    "subcategoryName": "chemical enginnering",
    "books": [
        {
            "bookName": "",
            "author": "",
            "ratings": [3, 4, 5, 3],
            "feedbacks": ["awesome book", "very amazing book"],
            "price": 222
        }
    ]
},
{
    "subcategoryName": "mechanical engineering",
    "books": [
        {
            "bookName": "",
            "author": "",
            "ratings": [3, 4, 5, 3],
            "feedbacks": ["awesome book", "very amazing book"],
            "price": 222
        }
    ]
}]

etc.

It really all depends on how your data is structured and how you pass it from your frontend but I think you got the idea.


Note that you got spelling in your sample data, please change 'feedbcks' to 'feedbacks'.

Sign up to request clarification or add additional context in comments.

3 Comments

I got the idea but how to send it in a POST request using Postman?
You can post data in body -> raw -> application/json, for example. Feel free to read more about how to use Postman: medium.com/@thejasonfile/…
Thank you for help

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.