I need to perform some modification on an API that returns something like this:
[
{
"_id": "0000000000000000001",
"name": "Random name",
"categories": [
"5eb8548330550e017f8902e6",
"5eb2548630550e117f8909eb",
"5eb6548930550e017f8909e9"
]
},
{...},
]
each results' categories are returned as ids of their respective documents. I need to manipulate the result so each object's categories field has an array of objects and each object has the id and the name of its category. I make an example of what the outcome should be:
[
{
"_id": "0000000000000000001",
"name": "Random name",
"categories": [
{"_id": "5eb8548330550e017f8902e6",
"name": "Category 1"},
{"_id": "5eb2548630550e117f8909eb",
"name": "Category 2"},
{"_id": "5eb6548930550e017f8909e9",
"name": "Category 3"},
]
},
{...},
]
I need to do this using plain JS and for so far this is what I have done but it returns an array of undefined:
const resultArray = await Promise.all(
searchResults.map(async (item) => {
item.categories.map(async (categoryId) => {
return await CategoryType.find({ _id: categoryId});
});
})
);
For the moment I am trying to get each category document for each id in the categories field. I bet the reason why I am getting an array of undefined is that I am handling async in the wrong way but cannot figure out how.