Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
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.

Why is this happening?

Thanks for your help!!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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();
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!