Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

148
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda