diff --git a/controllers/bootcampsController.js b/controllers/bootcampsController.js index 3fe90c1..c64dc36 100644 --- a/controllers/bootcampsController.js +++ b/controllers/bootcampsController.js @@ -72,7 +72,9 @@ exports.creteBootcamp = asyncHandler(async (req, res, next) => { // @access Private exports.updateBootcamp = asyncHandler(async (req, res, next) => { // try { - let bootcamp = await Bootcamp.findById(req.params.id); + const bootcampId = req.params.id; + const body = req.body; + let bootcamp = await Bootcamp.findById(bootcampId); if (!bootcamp) { return next( @@ -81,7 +83,7 @@ exports.updateBootcamp = asyncHandler(async (req, res, next) => { } //Make Sure user is bootcamp owner ********************************* - if (bootcamp.user.toString() !== req.user.id && req.user.role !== "admin") { + if (bootcamp?.user.toString() !== req.user.id && req.user.role !== "admin") { return next( new ErrorResponse( `User ${req.user.id} Is Not Authorized to Update The Bootcamp`, @@ -90,10 +92,14 @@ exports.updateBootcamp = asyncHandler(async (req, res, next) => { ); } - bootcamp = await Bootcamp.findByIdAndUpdate(req.params.id, req.body, { - new: true, - runValidators: true, - }); + bootcamp = await Bootcamp.findByIdAndUpdate( + bootcampId, + { ...body }, + { + new: true, + runValidators: true, + } + ); res.status(200).json({ success: true, data: bootcamp }); // } catch (errors) {