To err is human, to create error handling middleware is divine. This article will explore how to catch and handle errors in Express using Mongoose and then display the errors to the end user using React.

All code used in this article is in this repo.

This diagram shows the modules we’ll discuss in the article. This app is using the MERN stack. The server is running through an Express app, and is connected to a MongoDB database. The frontend is a React app. These are the files we’ll be using for error validation:

Image for post

Image for post

Steps described:

  1. Set up error handling middleware.
  2. Handle different errors.
  3. Send errors to the browser.
  4. Display error messages on the frontend.
  5. Highlight fields with errors on the frontend.

As you can see, this article has a few steps. It assumes some familiarity with Express, MongoDB/Mongoose, authentication, React/React router, and React hooks.

#node #mongodb #error-handling #frontend #react

Handling errors in Mongoose/Express for display in React
14.35 GEEK