Can anyone explain me
why await loadCartData(localStorage.getItem(“token”));
this works properly but when I use
await loadCartData(token);
it dosent work and gives an error
TypeError: Cannot read properties of undefined (reading ’66c1f742ebf65ca0bceabd04′)
Code:
const [token, setToken] = useState("");
useEffect(()=>{
async function loadData(){
await fetchFoodList();
if(localStorage.getItem("token")){
setToken(localStorage.getItem("token"));
await loadCartData(localStorage.getItem("token"));
}
}
loadData();
},[])
const loadCartData = async (token) => {
const response = await axios.post(url+"/api/cart/get",{}, {headers: {token}})
setCartItems(response.data.cartData);
}
I am trying to fetch cart data from the backend and it works when I use localStorage.getItem(“token”) but gives an error when passing the useState variable token
New contributor
Sandip Basak is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.