0

In my database, I have a project object with an hours property, which is just an array of length 6 with opening and closing times. The project hours are being loaded correctly (I've outputted it to handlebars). But when I try to loop through the project.hours array via Handlebars' each, I get TypeError: this.get is not a function.

{{project.hours}} <!-- this prints the array to the page, so I know it's right -->
{{#each project.hours}}
   {{this}} <!-- this throws an error -->
{{/each}}

Any ideas as to why this won't work?

Also, it's worth mentioning that the hours array looks like:

[{open: 8, close: 21}, 
{open: 8, close: 21}, 
{open: 8, close: 21}, 
{open: 8, close: 21}, 
{open: 8, close: 21}, 
{open: 8, close: 21}, 
{open: 8, close: 21}]

UPDATE

Here is the raw output of {{project}}

{ _id: 5748640ceadbd4d82a00000c, number: 11924, owner: 574118154f1c5e682800000c, title: 'New Project', description: 'This should be where we test editing.', hours: { '0': { close: '23', open: '17' }, '1': { close: '23', open: '17' }, '2': { close: '23', open: '17' }, '3': { close: '23', open: '17' }, '4': { close: '23', open: '17' }, '5': { close: '23', open: '17' }, '6': { close: '23', open: '17' } } }
  • Does it work if you use `{{this.open}} - {{this.close}}`? – Shakespeare May 27 '16 at 07:43
  • No, any reference to `{{this}}` throws an error. – Marcus Longwell May 27 '16 at 14:42
  • From what I can tell this should work... I even added it into my own Handlebars setup and it did work. Would you be able to post the entire contents of the data you are passing to the Handlebars template? – Shakespeare May 27 '16 at 14:52
  • @OwenAyres It's been updated to include the raw output of `{{project}}` – Marcus Longwell May 27 '16 at 15:18
  • It's because your `{{project}}` data for hours is not an array, which is what Handlebars is expecting when you say `each`. How are you passing this data into the Handlebars template? Ideally you should adjust the hours object so it is an array rather than an object. – Shakespeare May 28 '16 at 13:03
  • http://handlebarsjs.com/builtin_helpers.html#iteration According to this, objects should be able to be iterated by `each` as well. Nevertheless, I'll get it sent back as an actual array rather than an object and just call it good. Thanks! – Marcus Longwell May 28 '16 at 15:36
  • Sorry, you are right I keep forgetting they added this functionality. Unfortunately I can't see why it wouldn't work (does on my tests), so the only thing I can suggest is confirming you have the latest version of Handlebars runtime (or v4+ at least) and alternatively working around with array like you said, or implementing a helper (http://stackoverflow.com/questions/9058774/handlebars-mustache-is-there-a-built-in-way-to-loop-through-the-properties-of) – Shakespeare May 28 '16 at 16:09

0 Answers0