const ErrorHandler = (err, req, res, next) => {
logging.error(err);
let message = 'An error occured, please try again.';
let statusCode = 500;
const response = {
success: false,
message: message,
};
res.status(statusCode).json(response);
// in the future add custom error messages based on error type
};
export default ErrorHandler;
const ErrorHandler = (err, req, res, next) => {
logging.error(err);
let message = 'An error occured, please try again.';
let statusCode = 500;
const response = {
success: false,
message: message,
};
res.status(statusCode).json(response);
// in the future add custom error messages based on error type
};
export default ErrorHandler;
The above is my custom middleware. I have appropiately imported it on my main app.js file using:
const ErrorHandler = require('./middleware/ErrorHandler');
app.use(ErrorHandler);
And my post route looks like:
router.post('/submit-recipe', getUserMiddleware, upload.any(), async (req, res, next) => {
I have followed along some other posts such as this one, and all my routers are correctly being exported/imported.
Am I missing something obvious here?
Share Improve this question asked Mar 18 at 18:16 Hope Hope 11 6- EDIT: I was mixing Common.js exports and ES exports, which was not working... – Hope Commented Mar 18 at 18:37
- 1 You're mixing ES and CJS modules. require('./middleware/ErrorHandler') is likely an object, this depends on your setup, which the question doesn't mention. Can be fixed with module.exports = ErrorHandler – Estus Flask Commented Mar 18 at 18:39
- 1 @Hope then you'll want to probably delete your question, since you fixed things yourself mere minutes after asking for help, so you didn't really need help =) – Mike 'Pomax' Kamermans Commented Mar 18 at 19:35
- I disagree with the commenter above. Since its fine to ask and answer a question yourself if you already know the answer, certainly it is if you figure it out a few minutes after you post the question – Starship Remembers Shadow Commented Mar 18 at 20:09
- This is my first time actually posting to stack overflow. Can I answer my own questoin? oops – Hope Commented Mar 20 at 13:12
1 Answer
Reset to default -1This can be fixed by changing const ErrorHandler = require('./middleware/ErrorHandler');
to const ErrorHandler = module.exports = ErrorHandler
. You seem to have mixed up Common.js exports and ES exports.