I am new to javascript. I am looking for a way to retrieve the information from a local server running on Node:
*0
colors
0 "Tan"
1 "Chocolate"
2 "Black"
3 "White"
_id "5be9c8541c9d440000665243"
name "Norbert"
price 2900
imageUrl "http://localhost:3000/images/teddy_1.jpg"
description "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
1
colors
0 "Pale brown"
1 "Dark brown"
2 "White"
_id "5beaa8bf1c9d440000a57d94"
name "Arnold"
price 3900
imageUrl "http://localhost:3000/images/teddy_2.jpg"
description "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."*
I'm looking for a way to display them as HTML in a div. Here is my code for the moment, it does not display an error but I am unable to display the array elements ... Any ideas? Thanks for your suggestions!
const fetchPromise = fetch('http://localhost:3000/api/teddies');
const main = document.getElementById("clothes-box");
fetchPromise.then(response => {
return response.json();
}).then(products => {
main.appendChild = listOfNames(products);
})
function listOfNames(products) {
const listHtml = document.createElement('ul')
products.forEach(function(product) {
const listItem = document.createElement('li');
listItem.textContent = product.name
listHtml.appendChild(listItem)
})
return listHtml
}