我想建立一个从数据库返回产品的网站,当单击“查看更多”时,应该在另一个html页面中从服务器返回产品详细信息。问题是,当我单击“查看更多”时,productID%20 =%20null :(。
productDetails =第二个html页面。
productDetails = div-在index.html中,从服务器返回产品的位置
<script>
var productsUrlList = 'https://finalonlineshop.firebaseio.com/.json';
async function getProductsFromServer() {
var productsResponse = await fetch(productsUrlList)
var products = await productsResponse.json();
return products;
}
async function showProducts(productsPromise) {
var products = await productsPromise;
var generatedHTML = '';
var productsIDs = Object.keys(products);
productsIDs.forEach(productID => {
var product = products[productID];
generatedHTML += getGeneratedHTMLForProduct(productID, product);
});
document.getElementById('categories').innerHTML = generatedHTML;
}
function getGeneratedHTMLForProduct(productID, product) {
var generatedHTML = `
<div id = categoriesDiv>
<img class = "categoryImage" src = ${product.Image} />
<div class = "categoryName">${product.Name}</div>
<div class = "categoryPrice">$ ${product.Price}</div>
<br>
<button id = "seeMore" onclick = "seeMore('${productID}')">See
more</button>
</div>
`;
return generatedHTML;
}
function seeMore (productID) {
window.location = `./productDetails.html?productID = ${productID}`;//issue
}
function getProductIDFromUrl () {
var params = new URLSearchParams(window.location.search);
var productID = params.get('productID');
return productID;
}
async function getDetailsFromServer(productID) {
var detailsResponse = await fetch(`https://finalonlineshop.firebaseio.com/Products/details/${productID}.json`);
var details = await detailsResponse.json();
return details;
}
async function seeDetails(detailsPromise) {
var details = await detailsPromise;
var generatedHTML = `
<div id = "detailsAboutProduct">
<img src = "${details.Image}" /> //Cannot read property "Image" of null
<div>${details.Name}</div>
<div>${details.Details}</div>
<div>$ ${details.Price}</div>
<div>${details.Qty}</div>
<button id = "addToCart" onclick = "addToCart();">Add to
cart</button>
</div>
`;
document.getElementById('details').innerHTML = generatedHTML;
}
</script>
答案 0 :(得分:1)
消除URL中<div class="wrapper">
<!-- Leftside background -->
<div class="col s8 leftside">
</div>
<!-- Rightside background -->
<div class="col s4 rightside">
</div>
<!-- Article -->
<div class="col s6 offset-s3 article">
<a href="<?php echo $page->permalink(); ?>">
<h4>
<?php echo $page->title(); ?>
</h4>
</a>
<?php if ($page->coverImage()) : ?>
<img src="<?php echo $page->coverImage(); ?>" />
<?php endif ?>
<!-- Full content -->
<?php echo $page->content(); ?>
</div>
</div>
周围的空格,该空格被编码为=
。您还应该使用%20
来转义产品ID中的任何特殊字符。
encodeURIComponent()
答案 1 :(得分:0)
您对Firebase应用程序的查询似乎有误。
您正在获取:https://finalonlineshop.firebaseio.com/Products/details/${productID}.json
返回null
但您必须提取:https://finalonlineshop.firebaseio.com/${productID}.json
相反,它将返回正确的对象