I'm currently in the process of transitioning my requests to async calls to prevent them from blocking.
app.js
app.use(function(err, req, res, next) {
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
console.log(err);
res.status(err.status || 500);
res.json({ error: err.message });
});
route
router.get(
"/seatData/:seatNumber",
[
check("seatNumber")
.matches(/^[0-9][a-z]$/i)
.withMessage("must start with a number(0-9) and end with a letter")
],
actions.fetch_seat
);
controller
exports.fetch_seat = (req, res, next) => {
Seat.getSeatByNumber(req.params.seatNumber, (err, seat) => {
if (err) res.json(err);
else res.json(seat);
});
};
model
Seat.findSeat = function(key, value) {
return new Promise((resolve, reject) => {
const seat = file.find(r => r[key] === value);
if (!seat) {
reject({
error: true,
message: "Seat not found",
status: 404
});
}
resolve(seat);
});
};
Seat.getSeatByNumber = function(seatNumber, result, next) {
try {
this.seat = this.findSeat("seatNumber", seatNumber)
.then(seat => {
if (seat !== undefined && Object.keys(seat).length > 0) {
result(null, seat);
} else {
result({ error: true, message: "Seat not found" });
}
})
.catch(error => {
return;
});
} catch (error) {
console.log(error);
}
};
I've followed some online guides but can't seem to properly get the error response back to express. The code within /* ... */
is the original code I'm attempting to convert.
UPDATE
What is the difference between the 1st catch and the 2nd catch? Also, why doesn't my app.js catch the status code I returned?
Seat.findSeat = function(key, value) {
return new Promise((resolve, reject) => {
const seat = file.find(r => r[key] === value);
if (!seat) {
reject({
error: true,
message: "Seat not found",
status: 404
});
}
resolve(seat);
});
};
Seat.getSeatByNumber = function(seatNumber, result, next) {
try {
this.seat = this.findSeat("seatNumber", seatNumber)
.then(seat => {
console.log(seat);
if (seat !== undefined && Object.keys(seat).length > 0) {
result(null, seat);
} else {
result({ error: true, message: "Seat not found" });
}
})
.catch(error => {
console.log("1st catch");
return result(error);
});
} catch (error) {
console.log("outer catch");
console.log(error);
result(error);
}
};
app.js The errors are not being logged here
app.use(function(err, req, res, next) {
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
console.log('apperro' , err) // <-- does not log this
res.status(err.status || 500);
res.json({ error: err.message });
});
UPDATE 2
After adding next
, I encounter the following error. You can input seatData/1T
into the URL within the sandbox https://codesandbox.io/s/elucidate-api-fokd8
(node:319) UnhandledPromiseRejectionWarning: TypeError: next is not a function
at Seat.getSeatByNumber (/sandbox/src/controller/appController.js:15:14)
at findSeat.then.catch.error (/sandbox/src/model/seat.js:45:16)
(node:319) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:319) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.