Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JSON empty when is called from another file

I’m learning NodeJS and MongoDB. I dont know what is happening with this function.

getAllUsers = async () = {

let user;
let result = await new Connection().connect();
if (result.status == "ok")
    {
        user = await User.find();

    }
return user;
};

If I make a console.log before return user, it works fine (just print an JSON array with all info of the collection)

The problem is when I call it from another file (in my case, the router). If I do this, I receive an empty json.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Why is this happening?

Thanks for your help!!

>Solution :

When calling your function getAllUsers, you need to add await in front of it since it is an async function.

For example,

var listUsers = await getAllUsers();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading