expressjs / express

Fast, unopinionated, minimalist web framework for node.

Home Page:https://expressjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

how to render conditional templates in res.render()

moezbenrebah opened this issue · comments

commented

Hi everyone, I'm a student in web development, I built a web application, where I tried to implement in one of the route handler function a way to render 2 templates whenever its condition is fulfilled, but I didn't figure out how to reach my goal, so I tried like below, but it didn't work:

exports.myBookedTravels = catchAsyncHandler( async(req, res, next) => {
  // find all booked travels based on user id
  const bookedTravels = await Booking.find({ user: req.user.id });

  // find all travels that includes the above id (bookedTravels)
  const travelsIds = bookedTravels.map(item => item.travel);
  const travels = await Travel.find({ _id: { $in: travelsIds } });

  // Render the page that contains the booked travels || an empty page with a simple text
  if (!bookedTravels) res.render('nobooking')
  else {
    res.status(200).render('overview', {
      title: 'My travels',
      travels
    });
  }
});

Sorry, I'm a beginner and I try to find an adequate solution to my case but I didn't, and I'll appreciate your suggestions