Skip to content
Advertisement

How to fix async function which returns `undefined`?

I’m trying to return the count from my database. The count().exec method returns a Promise. I’m trying to resolve it in order to return the value to the user. But it returns undefined.

It seems to me I have well used the async/await pattern, so what is wrong? I can’t figure it out.

Here my snippet :

JavaScript

*** Edit *** : it’s now works, here’s my new snippet:

JavaScript

I’m just wondering if I have to wrap all the ulterior process inside my function call. So maybe some refactoring will occur if possible to avoid some hell-type process. Anyway, meanwhile it works, so great, thanks.

Advertisement

Answer

Your estimation() function returns undefined instead of returning a promise, that is because you return the new string from the callback function.

replace countQuery.then(...) with return countQuery.then(...).

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement