display api response in html in nodejs

Viewed 236

I hired a developer to help to create API. He displayed all results in <br> tag like so :

/public/index.ejs

function showResult() {
  item = window.items[window.currentPage];
  let result = '';
  for (i = 0 ; i < Object.keys(item).length ; i ++) {
    result = result + Object.keys(item)[i] + '=' + Object.values(item)[i] + '<br/>';
  }
  $("#result").html(result);
}

app.js

res.send(response['resp_result']['result']['products']['product']);

How do I display each object from the response in index.ejs like below ?

<div>price : {product_price} <span>{product_name}</div> 
1 Answers

replace

result = result + Object.keys(item)[i] + '=' + Object.values(item)[i] + '<br/>';

with

result = result + '<div>price : ' + Object.keys(item)[i] + ' <span>' + Object.values(item)[i] + '</div>';

except you don't have a closing tag for the <span/> - but I just made do what you put.

Also, I don't know which is the key and which is the value so you might have to swap Object.keys(item)[i] and Object.values(item)[i]

Related