View Full Version : Accessing an API using Handlebars.js

03-19-2018, 02:47 PM

I'm able to access this API and show that one product inside the API:

API for one product (https://codepen.io/itsthomas/pen/yKaeGB?editors=1010)

Now I want to display more than a product by accessing another API which is an array of products with the following code, but it doesn't work. What is the problem?

API for many products (https://codepen.io/itsthomas/pen/qoqMWz?editors=1010)


03-19-2018, 03:15 PM
OK, there could also be other problems. But your fetch address is missing the:


part. You have:


But it should be:


The reason I say there could also be other problems is that this returns an array of objects instead of a single object that the successful single product code does. But it at least looks like you're attempting to account for that. I'll try it in your pen and see what happens.

OK, just did that, and get a new error at least (the previous error was that nothing was fetched), and it appears to apply to what I was saying:

Must pass iterator to #each

Not sure how that part works in handlebar though. Might be able to figure it out, or maybe you can.

03-19-2018, 03:42 PM
OK, I lucked out. Here's a working demo (note, do not copy code from a message the forum may send you, it might be corrupted, instead, log on to the forum and copy directly from the post):

<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.11/handlebars.min.js"></script>
<ul id="container"></ul>

<script id="ourTemplate" type="text/x-handlebars-template">
{{#each this}}
<li class='product-container'>
<a href="https://matchesfashion.com{{ url }}" target="_blank">{{ name }}</a>
<div class='img-container'>
<img src=" {{ thumbnail }} ">
<div>Number in stock: {{ stock.stockLevel }}</div>
<div class='price-container'>Price: {{ priceData.formattedPriceWithoutDecimals }}</div>
.then(function(response) {
if (!response.ok) {
throw Error(response.statusText);
// Read the response as json.
// console.log( response.json());
return response.json();
.then(function(response) {
// Do stuff with the JSON
// console.log(response);
.catch(function(error) {
console.log('Looks like there was a problem: \n', error);

// Function to generate the HTML
function createHtml(ourData) { // ourData is just a parameter and can be named anything
var rawTemplate = document.querySelector("#ourTemplate").innerHTML;
var compiledTemplate = Handlebars.compile(rawTemplate);
var ourGeneratedHTML = compiledTemplate(ourData);

var ourContainer = document.querySelector("#container");
ourContainer.innerHTML = ourGeneratedHTML;

03-19-2018, 03:52 PM
Dear @John,
Thank you sooooooo much for your help and the time you invested to help me. I really appreciate it.
1000 Thanks again.

03-19-2018, 04:11 PM
You're welcome. BTW - not sure if you noticed it, but I had put up a live demo. Then I realized I was potentially participating in some e-commerce, so decided to take it down. All the code needed to get this to work is in my post though. Let me know if you need the live demo again, I can leave it up for a day or two if necessary.

03-19-2018, 04:17 PM
Sure, but the only thing I had to change in my example to make it work, was changing {{#each}} to {{#each this}} and it's working now:


PS: and of course updated the URL by adding cors-anywhere.herokuapp.com/ to it.

03-19-2018, 04:51 PM
Right. Those were the two things.