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

245
Visualizações
React - using express router, mongoose, which approach is better?

Giving by example the GET route request below, both WORK. I am trying to understand their differences and which one is the best approach for doing this. Any comments on the 2 coding options, please?

Option 1

router.get("/", (req, res) => {
   AddStudents.find({})
     .then((data) => {
       res.json(data);
     })
     .catch((error) => {
     });
 });

Option 2

router.get("/", async (req, res) => {
  AddStudents.find({}, (err, result) => {
    if (err) {
      res.send(err);
    }
    res.json(result);
  });
});
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

res.json will convert the data to json format. So it will convert the data to

res.json: content-type: application/json

from

res.send: content-type: text/html

option 2

async 

will automatically wrap the data in a promise so you dont have to do that.

So when an array is passed they return the same but if there are null or undefined values in the data the res.json will do explicit JSON conversion of them cuz aint valid json

over 4 years ago · Santiago Trujillo 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