I've got a problem on my JAVASCRIPT code. I've make a first page with some products. I would make a link to my second page when i click on the product.
But on my second page, there is an error : "script.js:35 Uncaught (in promise) TypeError: Cannot read property 'appendChild' of null at getProducts (script.js:35)"
I don't understand what i have to do.
HTML index.html (page 1):
<section id="indexCam" class="container">
<div class="row">
<div id="colCam1" class="col-12 col-lg-4"></div>
<div id="colCam2" class="col-12 col-lg-4"></div>
<div id="colCam3" class="col-12 col-lg-4"></div>
</div>
<div class="row">
<div id="colCam4" class="col-12 col-lg-4"></div>
<div id="colCam5" class="col-12 col-lg-4"></div>
</div>
</section>
HTML product.html (Page 2) :
<section class="container">
<div class="row">
<div class="col-12 col-lg-6 justify-content-center">
<img id="pictureProduct" src="" alt="" />
</div>
</div>
<div class="row">
<div class="col-12 text-center">
<h1>Produit :</h1>
<p id="nameProduct"></p>
</div>
</div>
<div class="row">
<div class="col-12 text-center">
<h2>Description :</h2>
<p id="textProduct"></p>
</div>
</div>
<div class="row">
<div class="col-12 text-center">
<h3>Choix de la lentille</h3>
</div>
</div>
<div class="row">
<div class="col-12 text-center">
<h4>Prix :</h4>
<p id="priceProduct"></p>
</div>
</div>
</section>
Javascript :
// Url produit
let urlProduct = "";
// Appel de produits index.html
const indexCam = document.getElementById("indexCam");
const colCam1 = document.getElementById("colCam1");
const colCam2 = document.getElementById("colCam2");
const colCam3 = document.getElementById("colCam3");
const colCam4 = document.getElementById("colCam4");
const colCam5 = document.getElementById("colCam5");
const cols = [colCam1, colCam2, colCam3, colCam4, colCam5];
const getProducts = async function() {
let response = await fetch("http://localhost:3000/api/cameras" + urlProduct)
let data = await response.json()
console.log(data)
for(i=0; i < data.length; i++) {
let indexCard = document.createElement("div");
let indexLink = document.createElement("a");
let indexImg = document.createElement("img");
let indexBodyCard = document.createElement("div");
let indexProductTitle = document.createElement("h5");
let indexProductPrice = document.createElement("p");
cols[i].appendChild(indexCard);
indexCard.classList.add("card");
indexCard.appendChild(indexLink)
indexLink.classList.add("stretched-link");
indexLink.appendChild(indexImg);
indexImg.classList.add("card-img-top");
indexLink.appendChild(indexBodyCard);
indexBodyCard.classList.add("card-body");
indexBodyCard.appendChild(indexProductTitle)
indexProductTitle.classList.add("card-title");
indexBodyCard.appendChild(indexProductPrice);
indexProductPrice.classList.add("card-text");
indexProductTitle.innerHTML = data[i].name;
indexProductPrice.innerHTML = parseInt(data[i].price / 100).toFixed(2) + " €";
indexLink.setAttribute("href", "product.html?"+ data[i]._id)
indexImg.setAttribute("src", data[i].imageUrl);
}
}
getProducts()
// Page Produit
// Création de l'URL
function product() {
if (document.getElementById("pictureProduct") != null) {
let url = window.location.search
const urlParams = new URLSearchParams(url)
urlProduct = urlParams.get("id")
document.getElementById("pictureProduct").setAttribute("src", data[i].imageUrl);
document.getElementById("nameProduct").innerHTML = data[i].name;
document.getElementById("textProduct").innerHTML = data[i].description;
document.getElementById("priceProduct").innerHTML = parseInt(data[i].price / 100).toFixed(2) + "€";
}
}
// Evenement on click
Error on the second page after click
Thanks for help
Nindra