So I have this array of strings which I'm using as filter to fetch documents from MongoDB using Mongoose. I'm successfully getting the data back from the db, but it is also returning Promise { <pending> }
on the console even though I'm wrapping the map function with Promise.all
.
Please note that this map function is nested inside two level deep for in
loops which I'm leaving out for simplicities sake.
Suppose this is the array of strings:
const subCategories = [ 'Politics', 'Entertainment' ];
And this is the map function:
const subCategoryID = await Promise.all( subCategories.map( async item => {
try {
const data = await SubCategory.findOne( { subCategory: item } );
return data;
} catch( e ) {
throw err;
}
}));
console.log( subCategoryID );
Promises make my head spin. I have tried learning Promises more than 5-6 times in the last 1-2 years and it still confuses me. So if you can, please provide a solution with Async/Await.