I get this error when im trying to display my json data in html it says undefined.
i thinks it’s because it cant find my goods.varer and goods.pris and goods.billede. How can i solve it. Please help, its for an exam project.
this is my code
varer.js
document.getElementById("clickMe").addEventListener('click', async() => {
let table = document.getElementById('varerTable');
let result = await fetch("http://localhost:8200/varer/getproducts", {method: 'GET', headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
})
.then(res => res.json())
.catch(console.log("error"));
let tableHtml = `<tr>
<th>varer</th>
<th>pris</th>
<th>billede</th>
</tr>
`;
for(const goods in result){
tableHtml += `
<tr>
<td>${goods.varer}</td>
<td>${goods.pris}</td>
<td><img src="${goods.billede}" style="height:50px;width:50px;"</td>
</tr>
`;
}
table.innerHTML = tableHtml;
});
varer-controller.js
router.get("/getproducts", (req, res) =>{
res.status(200).json(products)
console.log(products)
})
//vis varer for en kategori
router.get("/getproductsforenkategori/:varer", (req, res)=>{
let category = req.params.varer;
if(products[category]){
res.status(200).json({[category]:products[category]});
}
else{
res.sendStatus(404)
}
});
varer.json
[{"varer":"ss","pris":"sss","billede":""},{"varer":"ss","pris":"sss","billede":""}]