Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consol error - Cannot read properties of null #27

Open
Nishidhachaudhari opened this issue Apr 28, 2023 · 4 comments
Open

consol error - Cannot read properties of null #27

Nishidhachaudhari opened this issue Apr 28, 2023 · 4 comments

Comments

@Nishidhachaudhari
Copy link

cart Reducer.js:138
Uncaught TypeError: Cannot read properties of null (reading 'reduce')

@RealRajnish
Copy link

RealRajnish commented Jun 3, 2023

In my Case it was causing because of cart context
Change your code in cart context

const getLocalCartData = () => {
let localCartData = localStorage.getItem("thapaCart");
const parsedData = JSON.parse(localCartData);
if (!Array.isArray(parsedData)) return [];
return parsedData;
};

that should work

@raghub-github
Copy link

let getLocalCartData = JSON.parse(localStorage.getItem('userCartData')) ? JSON.parse(localStorage.getItem('userCartData')) : [];
replace it with this code,

@raghub-github
Copy link

cart Reducer.js:138 Uncaught TypeError: Cannot read properties of null (reading 'reduce')

let getLocalCartData = JSON.parse(localStorage.getItem('userCartData')) ? JSON.parse(localStorage.getItem('userCartData')) : [];

@dev-osiris
Copy link

The easiest way is to change the 2nd line (if condition) in the following code in cart_context.js:

  let localCartData = localStorage.getItem("thapaCart");  
  if (localCartData === []) {  
    return [];  
  } else {  
    return JSON.parse(localCartData); 
  }  
};

To:

if (localCartData.length === 0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants