Reputation: 1601
I am learning backend coding
I am using single collection to data save to db Collection name is book , each book has few nested arrays (like category and may be sub categories )
I have to show all categories title of each books in client side in category search scenario.
book = [
{
name: '';
----------
----------
category:[
{
title: ''
}
----------
----------
]
}
]
How to find and get data using mongoose ? Do I have to create separate collection for nested arrays ? is that right way?
// Get all category
exports.Allcategory = function(req, res){
Book.find({}, function(err, category){
if (err) return res.json({message: 'Error on the server!', status: 500 });
return res.json(category);
});
};
Upvotes: 0
Views: 50
Reputation: 463
In your Backend code, you are sending whole document of Book, not just category array. In your case this approach will be like this.
Books.map(item =>
{item.name} // this will print book name. in this iteration you can get any property defined at this level
{item.category.map(c => ){
{c.title} //this will print category name
}}
){
}
Upvotes: 0