thapatechnical / thapareactecom

535 stars 516 forks source link

consol error - Cannot read properties of null #27

Open Nishidhachaudhari opened 1 year ago

Nishidhachaudhari commented 1 year ago

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

RealRajnish commented 1 year ago

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 commented 1 year ago

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

raghub-github commented 1 year ago

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 commented 1 year ago

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)