Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions controllers/bootcampsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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`,
Expand All @@ -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) {
Expand Down