image problem
image problem
js**
No change is needed here, as the model seems fine for handling the image URL. But
I'll make sure the `image.url` is always passed correctly.
```javascript
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const Review = require("./review.js");
### 2. **Edit.ejs**
Make sure that the `image.url` field is correctly populated when editing. In your
form, you are handling the image URL input correctly. However, let's ensure that
the field is updated after editing.
```ejs
<%= layout("/layouts/boilerplate") %>
<div class="row mt-3">
<div class="col-8 offset-2">
<h3>Edit your Listing</h3>
<form method="POST" action="/listings/<%= listing._id %>?_method=PUT"
noValidate class="needs-validation">
<div class="mb-3">
<label for="title" class="form-label">Title</label>
<input name="listing[title]" value="<%= listing.title %>" type="text"
class="form-control" required>
<div class="valid-feedback">Title looks good</div>
</div>
<div class="mb-3">
<label for="description" class="form-label">Description</label>
<textarea name="listing[description]" class="form-control" required><%=
listing.description %></textarea>
<div class="invalid-feedback">Please enter a short description</div>
</div>
<div class="mb-3">
<label for="image" class="form-label">Image URL</label>
<input name="listing[image][url]" value="<%= listing.image.url %>"
type="text" class="form-control" required>
<div class="invalid-feedback">Please enter a valid link</div>
</div>
<div class="row">
<div class="mb-3 col-md-4">
<label for="price" class="form-label">Price</label>
<input name="listing[price]" value="<%= listing.price %>"
type="number" class="form-control" required>
<div class="invalid-feedback">Price should be valid</div>
</div>
<div class="mb-3 col-md-8">
<label for="country" class="form-label">Country</label>
<input name="listing[country]" value="<%= listing.country %>"
type="text" class="form-control" required>
<div class="invalid-feedback">Country name should be valid</div>
</div>
</div>
<div class="mb-3">
<label for="location" class="form-label">Location</label>
<input name="listing[location]" value="<%= listing.location %>"
type="text" class="form-control" required>
<div class="invalid-feedback">Location should be valid</div>
</div>
<button class="btn btn-dark edit-btn mt-3">Edit</button>
</form>
</div>
</div>
```
### 3. **Show.ejs**
Ensure the image is displayed by properly accessing the `image.url` in the `img`
tag. The rest of your code for the show page looks good.
```ejs
<%= layout("/layouts/boilerplate") %>
<div class="row">
<div class="col-8 offset-3 mt-3">
<h3><%= listing.title %></h3>
</div>
<div class="card col-6 offset-3 show-card listing-card">
<img src="<%= listing.image.url %>" class="card-img-top show-img"
alt="listing_image">
<div class="card-body">
<p class="card-text">
<%= listing.description %><br>
₹ <%= listing.price.toLocaleString("en-IN") %><br>
<%= listing.location %><br>
<%= listing.country %>
</p>
</div>
</div>
<br>
<div class="btns">
<a href="/listings/<%= listing._id %>/edit" class="btn btn-dark col-1
offset-3 edit-btn">Edit</a>
<hr>
<p><b>All Reviews</b></p>
<div class="row">
### 4. **Routes.js**
No changes are needed for the routes. The current route handling for `GET`, `POST`,
and `PUT` for listings should work fine. The image URL is updated in the database
when the form is submitted.
```javascript
const express = require("express");
const router = express.Router();
const wrapAsync = require("../utils/wrapAsync.js");
const ExpressError = require("../utils/ExpressError.js");
const { listingSchema, reviewSchema} = require("../schema.js");
const Listing = require("../models/listing.js");
// Index Route
router.get("/", wrapAsync(async (req, res) => {
const allListings = await Listing.find({});
res.render("listings/index.ejs", { allListings });
}));
// New Route
router.get("/new", (req, res) => {
res.render("listings/new.ejs");
});
// Show Route
router.get("/:id", wrapAsync(async (req, res) => {
let {id} = req.params;
const listing = await Listing.findById(id).populate("reviews");
res.render("listings/show.ejs", { listing });
}));
// Create Route
router.post("/", validateListing, wrapAsync(async (req, res, next) => {
const newListing = new Listing(req.body.listing);
await newListing.save();
res.redirect("/listings");
}));
// Edit Route
router.get("/:id/edit", wrapAsync(async (req, res) => {
let {id} = req.params;
const listing = await Listing.findById(id);
res.render("listings/edit.ejs", { listing });
}));
// Update Route
router.put("/:id", validateListing, wrapAsync(async (req, res) => {
let {id} = req.params;
await Listing.findByIdAndUpdate(id, { ...req.body.listing });
res.redirect(`/listings/${id}`);
}));
// Delete Route
router.delete("/:id", wrapAsync(async (req, res) => {
let {id} = req.params;
let deletedListing = await Listing.findByIdAndDelete(id);
console.log(deletedListing);
res.redirect("/listings");
}));
module.exports = router;
```