0

As shown below, I want to render a template for each topic in an array (this will have 50ish topics. Within the template, I need a variable set to the topic name when the user navigates to that path so that I can use that variable value to query a DB to render the correct items on the page. Right now, I'm just trying to log that variable, but it's not logging.

topics.forEach((topic) => {
  app.get(
    '/' + topic,
    function (req, res, next) {
      res.render('template', {
        topic: 'welcome to ' + topic,
      });
      next();
    },
    function (req, res) {
      currentTopic = topic;
      console.log(currentTopic);
    }
  );
});
  • you **SHOULD NOT** use a for loop to create your endpoints, use one endpoint with url parameter to get topic. check [express url parameters](https://stackoverflow.com/questions/20089582/how-to-get-a-url-parameter-in-express) for details. – mahdi lotfi Oct 01 '22 at 20:52
  • In your template, you can define the variable inside a ` – jfriend00 Oct 01 '22 at 20:52
  • I tried this, but it throws an error`` – Jeff Froehlich Oct 01 '22 at 21:30

0 Answers0