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

168
Visualizações
Problem with handling errors in Express framework

I am currently working on node.js + express + mongoDB project. I am trying to handle error that occurs when data cannot be received from database. I am simulating this by terminating mongod process in console and calling .get in Postman. Sadly instead of getting an error in Postman I only get Unhandled Promise Rejection in console. I read a lot of posts about error handling and implemented it according to this guide: https://expressjs.com/en/guide/error-handling.html. I would be grateful for any idea of how can I fix this.

The code:

Printing all courses:

router.get("/", async (req, res, next) => {
  try {
    const courses = await Course.find().sort("dishName");
    res.send(courses);
  } catch (ex) {
    next(ex);
  }
});

error.js:

module.exports = function (err, res, req, next) {
  res.status(500).send(`500 Error`);
};

index.js

const error = require(`./middleware/error`);

app.use(error);

app.use(error) is placed as the last app.use

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

There is a minor mistake in your code. The order of the req and res parameters in the error handler function should not be changed.

// Error.js
module.exports = function (err, req, res, next) {
  res.status(500).send(`500 Error`);
};
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