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

202
Visualizações
find feature for the mongodb module is not working in my Nextjs app

I have a nextjs application I have been developing and need to find all the documents in one of my collections I am using the mongodb node module. when i use .find() to pull the documents I just get an empty JSON object. I use .findOne, .updateOne with out any issues for my login and password reset pages. here is my code from the api.

     try{
      const client = await connectDatabase();
      const events = client.db().collection('members');
      const documents = events.find()
      res.status(200).json( documents);}catch(error){
      res.status(500).json({message: error.message});}

I have looked all over the mongodb documentation and have not found a solution to this issue. this is what I get back when I run the code {"_events":{},"_eventsCount":0}

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

MongoDB query is asynchronous, so you need to wait for the response. Change your code like this:

const documents = await events.find();
about 4 years ago · Juan Pablo Isaza Relatório

0

It's because .find() returns a cursor so you can iterate it, the best you can do here is transform it into an array like below:

const documents = await events.find().toArray();
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