Skip to content

how to render conditional templates in res.render() #4836

@moezbenrebah

Description

@moezbenrebah

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions