Skip to content
Advertisement

Nodejs/Express/JSON/Handlebars loop through array not working with multiple values

I am having problems understanding how to loop through an object in Handlebars and possibly passing information from one place to another.

Below is a sample json file which I need to read from. In this example, it’s just a bunch of image file names that I want to pull in.

This json file is called “testdata.json” and it has:

JavaScript

And in app.js, I require it to a variable by the same name testdata.

JavaScript

Then I pass it in alongside “app” to my homepage route file via

JavaScript

I want to get the array with image filenames from testdata, pass it in to the homepage (index.hbs) via a route, and then let handlebars iterate over the array to form a mini image gallery.

Here is what my sample routes file for the homepage looks like:

JavaScript

When I do:

Handlebars in index:

JavaScript

The images don’t appear, and when I look at the back end code via the console, I see this come out:

JavaScript

The filename never comes out. When testing and trying to console.log(TestWholeArray ); in terminal, I do see and get this:

JavaScript

To minimize the test even further, when I take the same test above, but instead of using this:

JavaScript

I dig a little deeper into the array to pull in just one image as oppose to all of them via:

JavaScript

Then it does work and the image does appear. It’s when I try to pass in more than one that it goes bonkers.

What am I doing wrong or what am I missing?

Advertisement

Answer

When looping an array, use {{this}} to access the current value.

JavaScript

What you were trying to do, is to access the property artistArtwork from the array artistArtwork which of course, doesn’t exist.


Working demo:

JavaScript
JavaScript
Advertisement