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

410
Vistas
promise doesn't work, how to use catch in this way?

I'm trying to use promise to make error message to the console when Mongo connection doesn't work. i get an error: Cannot read properties of undefined (reading 'catch') how can i fix it? it supposed to work like that. i can use try & catch block but i prefer to know how to use it this way and what is wrong

mongoose
  .connect(DB, {
    useNewUrlParser: true,
    useCreatIndex: true,
    useFindAndModify: false,
  })
  .then(() => {
    console.log('DB conecttion successful')
    .catch(() => console.log('Error'));
  });

const port = 3000;
app.listen(port, () => {
  console.log(`app runing on port ${port}`);
});
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

catch should be after then not after console.

console.log will return undefined as it void function, that why the error "Cannot read properties of undefined" appear

mongoose
  .connect(DB, {
    useNewUrlParser: true,
    useCreatIndex: true,
    useFindAndModify: false,
  })
  .then(() => {
    console.log('DB conecttion successful')
  }).catch(() => console.log('Error'));;

const port = 3000;
app.listen(port, () => {
  console.log(`app runing on port ${port}`);
});
about 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