Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

244
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda