Home > Enterprise >  express .get method callback function problem
express .get method callback function problem

Time:12-03

code for tour.route.js :

app.get(tour_controller.getTourById)

tour_controller.js
getTourById = handler.getOne(TourModel);

handler.js

exports.getOne = (Model, popOptions) => {
   catchAsync(async (req, res, next) => {
      let query = Model.findById(req.params.id);
      if (popOptions) {`your text`
         query = query.populate(popOptions);
      }
      const docs = await query;
      if (!docs) {
         return next(
            new apiError('No documents found with ID: '   req.params.id, 404)
         );
      }
      res.status(200).json({
         status: 'succeed!',
         data: docs,
      });
   });
};

problem that displays during npm start

I stuck here, the same thing works for .post, .patch and .delete method but it shows error for .get method

CodePudding user response:

You forgot to return

exports.getOne = (Model, popOptions) => {
   return catchAsync(async (req, res, next) => {
      // ...
   });
};
  • Related