I am working on extracting all child elements from MongoDB using a recursive function. The function loops through all the values and successfully logs them when pushed to an array. However, I am facing an issue where the returned array is empty. This code is being implemented in an Express.js environment. See the code snippet below:
static async getAllChildCat(categoryId){
var allCat = [];
let test = async (categoryId) => {
let category = await NewCategory.find({ 'parent': categoryId });
if (category.length > 0) {
await category.forEach(async elem => {
let newVal = await test(elem._id);
console.log(elem);
allCat.push(elem);
});
}
}
var val = await test(categoryId);
return allCat;
}