Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the Review Submission Error #100

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 1 addition & 5 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ app.use((req, res, next) => {
res.locals.success = req.flash('success');
res.locals.error = req.flash('error');
res.locals.currUser=req.user; //storecurrent session user info in currUser
console.log(res.locals);
// console.log(res.locals);
next();
});

Expand Down Expand Up @@ -180,10 +180,6 @@ const listingController = require('./controllers/listing.js');
// app.get("/new",isLoggedIn, asyncwrap(newpost));
app.get("/listing/new", isLoggedIn, asyncwrap(listingController.newpost));

//create new listing
// Create new listing form route
app.get("/listing/new", isLoggedIn, asyncwrap(listingController.newpost));


//index route
app.get("/listing",asyncwrap(index));
Expand Down
43 changes: 27 additions & 16 deletions controllers/reviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,39 @@ const listing=require("../models/listing.js");
const {
SUCCESS_REVIEW_ADDED,
ERROR_LISTING_NOT_FOUND
} = require('../constants.js')
} = require('../constants.js');

//add review
module.exports.reviewPost = (async (req, res) => {
const { id } = req.params;
const list = await listing.findById(id).populate("reviews");
const list = await listing.findById(id);

if (!list) {
req.flash('error', ERROR_LISTING_NOT_FOUND);
return res.redirect('/listing');
try{
if (!list) {
req.flash('error', ERROR_LISTING_NOT_FOUND);
return res.redirect('/listing');
}

const newReview = new review(req.body.review);
newReview.author = req.user._id;
console.log("author is", newReview.author);
list.reviews.push(newReview);

await newReview.save();
await list.save();
console.log(list)
req.flash('success', SUCCESS_REVIEW_ADDED);
res.redirect(`/listing/${list.id}`);
}catch(e) {
console.error(e);
req.flash('error', 'An error occurred while adding review.');
console.log(e);
// res.redirect(`/listing/${id}`);
}

const newReview = new review(req.body.review);
newReview.author = req.user._id;
list.reviews.push(newReview);

await newReview.save();
await list.save();

req.flash('success', SUCCESS_REVIEW_ADDED);
res.redirect(`/listing/${list._id}`);
});

//delete a review
module.exports.deleteReview =(async (req,res) =>{

let {id,rid} =req.params;
await listing.findByIdAndUpdate(id,{$pull:{reviews:rid}}); //update the listing-reviews array where review id matched rid
await review.findByIdAndDelete(rid); //deconstructing parameters
Expand Down
4 changes: 1 addition & 3 deletions views/show.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,7 @@
class="card listing-card card-body col-lg-11 col-md-8 col-sm-10 col-xs-10 offset-lg-3 offset-md-2 offset-sm-1">

<!-- Form for add reviews. -->
<form action="/listings/<%= list._id %>/review" method="post" novalidate
class="needs-validation">

<form action="/listing/<%= list._id %>/review" method="post" novalidate class="needs-validation">
<div>
<label for="rating" class="form-label bold2">Rating:</label>
<fieldset class="starability-grow">
Expand Down
Loading