Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Data mapping react js

Hello so i’m working on a project where i have thru data and displayed i’m having a problem cause the data isn’t consistante like for example not all product have sale i want to display the old price and the new one on the product detail page the product having a sale works fine however the ones that doesn’t i keep getting " Cannot read properties of undefined (reading ‘old_price’)" this is the code i’m currently using:

        <div >
        {filters.filter((product) => product.id === id).map((product) => (
          <div className='product-bigger-wrapper'>
          
            <div class="product-list-wrapper"> 
              <div className="product-detail-container">              
                <div>                    
                  <div className="image-container">
                    <img src={product.image} className="product-detail-image" alt=""/>
                  </div>            
                </div>
                <div className="product-detail-desc">
                  <div className="product-detail-header-title">
                    <h1 className='product-name'>{product.name}</h1>
                  </div>
                  <p className='product-unit-title'> {product.measurement}</p>
                  <div className="product-unit-price-container">
                    
                    <p className='product-unit-price'> ${product.price}</p>
                    { product.sale.old_price !== undefined ?
                      <p className='product-unit-wasprice'> ${product.sale.old_price}</p>
                      : null}


                  </div>              
                  <h4 className='product-description-p'>Product Details: </h4>
                  <div className='description'>
                    <p className='product-description'>{product.description}</p>            
                  </div>          
                </div>
              </div>
          </div>
         
        </div> 
       ))}       
  </div>  

and this is an example of product that has the sale part and product that doesn’t enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The problem is the line where you check for old_price being undefined.

{ product.sale.old_price !== undefined ? <p>...</p> : null }
           ^
           this is undefined in your second product object

You can fix it by checking for the sale object first

{ product.sale && product.sale.old_price !== undefined ? <p>...</p> : null }

or preferably use optional chaining

{ product.sale?.old_price !== undefined ? <p>...</p> : null }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading