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);
}