Hichame Yessou
Hichame Yessou

Reputation: 2708

Handlerbars access Array variable

I'm working with Handlebars in an Express application. From the server-side I'm passing an array of dates as strings:

reqCtrl.dateController = async (req, res) => {
...
var dates = [
    "2020-09-27",
    "2020-09-28",
    "2020-09-29",
    "2020-09-30",
    "2020-10-01",
    "2020-10-02",
    "2020-10-03",
    "2020-10-04",
  ];

  console.log(dates);
  //Resulting in: ['2020-09-27', '2020-09-28', '2020-09-29', '2020-09-30', '2020-10-01', '2020-10-02', '2020-10-03', '2020-10-04']
  res.render("requests/...", 
    dates
  });
}

However, on the client side, with the following expression I get different values.

<script>
console.log({{ dates }})
//Resulting in:  1984 1983 1982 1981 2009 2008 2007 2006
</script>

Am I missing something? How can I render the correctly formatted date?

Upvotes: 1

Views: 42

Answers (1)

Adam Specker
Adam Specker

Reputation: 422

What would happen if you rendered <p>{{dates}}</p>, instead of console logging it? Either way, you will likely want to iterate through arrays to render when using handlebars. This is done with #each.

<ul>
  {{ #each dates}}
    <li>{{this}}</li>
  {{/each}}
</ul>
  

Upvotes: 1

Related Questions