0% found this document useful (0 votes)
7 views3 pages

Correct Review

Uploaded by

hspandit071
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Correct Review

Uploaded by

hspandit071
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

The error `"Cannot read property 'undefined' reading redirectURL"` occurs because

`req.params.id` is not being properly passed to the `createReview` function in your


**controller/review.js** file.

### Updated Code:

#### ROUTE/REVIEW.JS
Make sure the route includes the `id` parameter for the listing.

```javascript
const express = require("express");
const router = express.Router({ mergeParams: true }); // Ensure access to parent
route params
const wrapAsync = require("../utils/wrapAsync.js");
const { validateReview, isLoggedIn } = require("../middleware.js");
const reviewController = require("../controllers/review.js");

// Create Review Route


router.post("/", isLoggedIn, validateReview,
wrapAsync(reviewController.createReview));

// Delete Review Route


router.delete("/:reviewId", isLoggedIn, wrapAsync(reviewController.destroyReview));

module.exports = router;
```

#### CONTROLLER/REVIEW.JS

Ensure you use the correct syntax for the `Listing` model and access the
`req.params.id` correctly.

```javascript
const Listing = require('../models/listing.js');
const Review = require("../models/review.js");

module.exports.createReview = async (req, res) => {


const { id } = req.params; // Access the listing ID from route params
const listing = await Listing.findById(id);
const newReview = new Review(req.body.review);
newReview.author = req.user._id;

listing.reviews.push(newReview);

await newReview.save();
await listing.save();

req.flash("success", "New Review Created");


res.redirect(`/listings/${listing._id}`);
};

module.exports.destroyReview = async (req, res) => {


const { id, reviewId } = req.params; // Access listing ID and review ID from
route params
await Listing.findByIdAndUpdate(id, { $pull: { reviews: reviewId } });
await Review.findByIdAndDelete(reviewId);

req.flash("success", "Deleted Review");


res.redirect(`/listings/${id}`);
};
```

#### APP.JS (Ensure Routes Are Nested Properly)


When setting up routes, make sure to pass the `id` of the listing in the parent
route to ensure the parameters are available in nested routes.

```javascript
const reviewRoutes = require('./routes/review');

// Listings Routes
app.use('/listings/:id/reviews', reviewRoutes);
```

#### SHOW.EJS
Make sure the form action includes the correct listing ID to pass it to the route.

```html
<form action="/listings/<%= listing._id %>/reviews" method="POST" novalidate
class="needs-validation">
<div class="mb-3 mt-3">
<label for="rating" class="form-label">Rating</label>
<fieldset class="starability-slot">
<input type="radio" id="no-rate" class="input-no-rate"
name="review[rating]" value="0" checked aria-label="No rating." />
<input type="radio" id="first-rate1" name="review[rating]" value="1" />
<label for="first-rate1" title="Terrible">1 star</label>
<input type="radio" id="first-rate2" name="review[rating]" value="2" />
<label for="first-rate2" title="Not good">2 stars</label>
<input type="radio" id="first-rate3" name="review[rating]" value="3" />
<label for="first-rate3" title="Average">3 stars</label>
<input type="radio" id="first-rate4" name="review[rating]" value="4" />
<label for="first-rate4" title="Very good">4 stars</label>
<input type="radio" id="first-rate5" name="review[rating]" value="5" />
<label for="first-rate5" title="Amazing">5 stars</label>
</fieldset>
</div>
<div class="mb-3 mt-3">
<label for="comment" class="form-label">Comments</label>
<textarea name="review[comment]" id="comment" cols="100" rows="7"
class="form-control" required></textarea>
<div class="invalid-feedback">Please submit some comments for the
review</div>
</div>
<button class="btn btn-outline-dark">Submit</button>
</form>
```

#### MODEL/REVIEW.JS
No change is required here, but ensure proper casing in your schema (`comment`
instead of `Comment`):

```javascript
const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const reviewSchema = new Schema({


comment: String, // Fixed lowercase
rating: {
type: Number,
min: 1,
max: 5,
},
createdAt: {
type: Date,
default: Date.now,
},
author: {
type: Schema.Types.ObjectId,
ref: "User",
},
});

module.exports = mongoose.model("Review", reviewSchema);


```

---

You might also like