0

I was able to verify that I can get the image object from firestore and all the information is rendered correctly without any errors on the console. I just can't display the image

I have the code below with it's imports.

import React, {useEffect, useState} from "react";
import Layout from "../components/Layout";
import { collection , getDocs } from "firebase/firestore";
import fireDB from "../fireConfig";
import { fireproducts } from "../firecommerce-products";
import { useNavigate } from "react-router-dom";

function Homepage() {
    const [products, setProducts] = useState([]);
    const navigate = useNavigate()
    useEffect(() =>{
        getData();
    },[]); 

    async function getData() {
            try {
                const product = await getDocs(collection(fireDB, "products"));
                const productsArray = [];
                product.forEach((doc) => {
                    const obj = {
                        id: doc.id,
                        ...doc.data(),
                    };
                    productsArray.push(obj);
                });
               setProducts(productsArray);
               console.log(productsArray);
            } catch (error) {
                console.log(error);
            }           
    }
    return (
        <Layout>
            <div className="container">
                <div className="row">
                    {products.map((product) => {
                        return <div className="col-md-4" key={product.id}>
                            <div className="m-2 p-1  product position-relative" >
                                <div className="product-content" >
                                    <p>{product.name}</p> 
                                    <div className="text-center">

On the first line below is the img tag that I used for the imageURL

                                    <img src={ product.imageURL } alt={product.description} className="product-img" />

                                    </div>
                               </div>
                                <div className="product-actions" >
                                    <h2>{product.price} R/-</h2>
                                    <div className="d-flex">
                                            <button>ADD TO CART</button>
                                            <button onClick={() =>{
                                            navigate('/productInfo/${product.id}') 
                                            }}>VIEW</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    })} 
                </div>
            </div>
     
        </Layout>
    )
}

export default Homepage;
isherwood
  • 58,414
  • 16
  • 114
  • 157
  • `setProducts` does not update the state immediately. See this [question](https://stackoverflow.com/questions/38558200/react-setstate-not-updating-immediately) for details. – tromgy Jan 04 '22 at 13:16
  • What is the value of `src` that ends up in the HTML element here: ` – Frank van Puffelen Jan 04 '22 at 14:35
  • @FrankvanPuffelen It is the path of the image in firestore – Zweli Sangweni Jan 05 '22 at 08:08
  • I understand its purpose, but would like to see its actual value, as copy/pasted from the `img` element in your HTML. – Frank van Puffelen Jan 05 '22 at 15:56
  • @FrankvanPuffelen So its basically `array` values and these are the values of one of the array objects shown on the console with the `img` element below category: "fashion" description: "" id: "9t6uxC4By4PkZstubMnE" imageUrl: "https://firebasestorage.googleapis.com/v0/b/firecommerce-c785d.appspot.com/o/fireproducts%2Ffabian-albert-AvnXTPOPVHY-unsplash.jpg?alt=media&token=cba3f464-6b15-4223-958d-3b3351655a17" name: "Dennis Lingo Mens Solid Fit Casual Shirt" price: 800 – Zweli Sangweni Jan 06 '22 at 15:01

1 Answers1

0

I have found the solution now, I had the imageurl field as imageUrl in firestore and had it as imageURL on the object when I retrieved it.This mismatch of the words resulted in the images not rendering. After I fixed that I was able to display the images. It was a stupid mistake on my side but I`m glad I fixed it