FLASH FAILURES (ALERTS)

 WHENEVER, A NON-EXISTING LISTING IS TRIED TO BE ACCESSED OR EDITED AN ALERT IS GENERATED.

FLASH.EJS


<% if(error.length){ %>
    <div class="alert alert-danger alert-dismissible fade show col-6 offset-3" role="alert">
      <%=error%><button
        type="button"
        class="btn-close"
        data-bs-dismiss="alert"
        aria-label="Close"
      ></button>
    </div>
    <%} %>

LISTING.JS

// SHOW ROUTE
router.get(
  "/:id",
  wrapAsync(async (req, res) => {
    let { id } = req.params;
    const listing = await Listing.findById(id).populate("reviews");
    if (!listing) {
      req.flash("error", "Listing requested doesn't exist!");
      res.redirect("/listings");
    }
    res.render("listings/show.ejs", { listing });
  })
);

// EDIT ROUTE
router.get(
  "/:id/edit",
  wrapAsync(async (req, res) => {
    let { id } = req.params;
    const listing = await Listing.findById(id);
    if (!listing) {
      req.flash("error", "Listing requested doesn't exist!");
      res.redirect("/listings");
    }
    res.render("listings/edit.ejs", { listing });
  })
);

IN APP.JS

app.use((req, res, next) => {
  res.locals.success = req.flash("success");
  res.locals.error = req.flash("error");
  next();
});













Comments