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

359
Visualizações
Express mongodb find() return nothing but get 200

I want to return all data in MongoDB, it returns 200, but I cannot get all the data properly.

I am using Express

I am learning to use MongoDB and doing homework for practice. The task just told me to set up the API for MongoDB, they gave me an HTML with a button to test the API, and the HTML will show the API success or not, I cannot check on the HTML.

The task that I fail is told me to get all data from MongoDB.

Here is the task which I fail

GET /api/products . Returns all products in the database as { products: Product[] }

I success to post data to MongoDB, when I use 'app.get()' to get all my data from MongoDB it fail, but I got 200 for the return, and I can see some data in the log.

That is the app.get() that I write

app.get('/api/products', (req, res, next) => {
    Product.find().then(
      (product) => {
        res.status(200).json({product});
      }
    ).catch(
      (error) => {
        res.status(404).json({
          error: error
        });
      }
    );
  }); 
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The below code seems to work fine for me. Please check it out.

app.get('/api/products', (req, res, next) => {
    Product.find({})
    .exec()
    .then(
      (product) => {
        res.status(200).json({product});
      }
    ).catch(
      (error) => {
        res.status(404).json({
          error: error
        });
      }
    );
  }); 
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