If I send a valid fetch api query to this Express server it works fine. If I send an invalid query the server crashes (ReferenceError: next is not defined). How can I change this so that if an error occurs;
- the server does not crash
- the client receives an error message from the server
Express server.js:
// Add a new test-resource-a
app.post('/test-resource-a', (request, response) => {
pool.query('INSERT INTO my_table SET ?', request.body, (error, result) => {
if (error) {
next(err);
}
response.status(201).send(`test-resource-a added with id: ${result.insertId}`);
});
});
//An error handling middleware
app.use(function (err, req, res, next) {
res.status(500);
res.send("Oops, something went wrong.")
});