Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

95
Views
React : Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'error')

Here is my code for Home.js and it is giving me an error regarding properties of undefined (reading 'error'). Can please anyone suggest a solution for this?

Here is the code in Home.js

import React from 'react'
import  {useState , useEffect} from 'react'
import {getProducts} from "./helper.js/coreapicalls"

function Home() {

    const [products, setProducts] = useState([]);
    const [error, setError] = useState(false);

    const loadAllProducts = () => {
        getProducts()
        .then((data) => {
            if (data.error) {
            setError(data.error);
            console.log(error);
            } else {
            setProducts(data);
            }
        });
    };

    useEffect(() => {
        loadAllProducts();
    });

    return (
        <div>
            <h1>Home component</h1>
            <div className = "row">
                {products.map((product , index)=>{
                    return(
                        <div key = {index}>
                            <h1>{product.name}</h1>
                        </div>
                    )
                })}
            </div>
        </div>
    )
}

export default Home

× Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'error')

  10 | const loadAllProducts = () => {
  11 |     getProducts()
  12 |     .then((data) => {
> 13 |         if (data.error) {
     | ^  14 |         setError(data.error);
  15 |         console.log(error);
  16 |         } else {
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

your data is probably undefined, hence reading error property on an undefined fails on line 13 if (data.error)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!