I am trying to avoid the try catch block in route handlers. I am trying to catch the error in the following manner:
const catchAsync = (fn) => {
// why this function does not have access to req,res,next?
// I am passing async(req,res,next) as an argument, hence
// req,res,next should be known to this function
fn(req, res, next).catch((err) => next(err));
};
and then in the route handler:
exports.createTour = catchAsync(async (req, res, next) => {
const newTour = await Tour.create(req.body);
res.status(201).json({
status: "success",
data: {
tour: newTour,
},
});
});
Now the problem is that, I don't understand why the function fn(req,res,next) inside the catchAsync function block, does not have access to (req,res,next) when called ?
All I understand is that I am passing async(req,res,next) as an argument to catchAsync function. Hence when the function gets called inside the catchAsync block, it should have access to req,res,next
createTour? Is it being used in an express route?