How do I get data back from MongoDB callback in NodeJS?

async function myAsyncFunc(monthlyPeriodsArr, monthly_periods_results_arr, monthly_periods_plot_arr) 
for (let index = 0; index < monthlyPeriodsArr.length; index++) {
const elementArr = monthlyPeriodsArr[index];
console.log(index+": element: "+elementArr);

var PromiseResult = await 
CallMongoDBWithPeriodResolution.executeMongoDBAggregateQueryDates(elementArr[0], elementArr[1]);
console.log("PR: "+PromiseResult);
console.log("PromiseResult: "+PromiseResult.then((result) => {
console.log("Success", result);

}).catch((error) => {
console.log("Error", error);



In the above code CallMongoDBWithPeriodResolution.executeMongoDBAggregateQueryDates(start_date, end_date) returns undefined.
But the query works in the function callback that connects to the database.
I can do any and all things with the data in the callback but returning an array or a promise always returns undefined. How can I correct this?
I have tried promises and await and async. I have been attempting regular functions while waiting for the callback.
Returning myresultsArray that is visible inside the callback returns undefined in all cases.


I have solved the issue using async, await and try/catch blocks.
But I am glad to use this forum and will ask again if the need arises. :smiley:

Happy coding to you all,

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.