bradtraversy / proshop_mern

Shopping cart built with MERN & Redux
1.99k stars 1.18k forks source link

Can't access cart. "TypeError: cartItems.map is not a function" #60

Closed NickSchmitt closed 3 years ago

NickSchmitt commented 3 years ago

I cloned your finished repo, and it's fully functioning except I receive the following error when trying to access the cart.

TypeError: cartItems.map is not a function
CartScreen
src/screens/CartScreen.js:41
  38 |     Your cart is empty <Link to='/'>Go Back</Link>
  39 |   </Message>
  40 | ) : (
> 41 |   <ListGroup variant='flush'>
     | ^  42 |     {cartItems.map((item) => (
  43 |       <ListGroup.Item key={item.product}>
  44 |         <Row>

All I have done is npm install and add my Mongo URI to .env.

Any idea why I might be running into this error?

aorelhas commented 3 years ago

Could you try to use a postman and hit this route? get(/api/products/${id});

basir commented 3 years ago

I clone the repo and set the .env file and it works. please open an Incognito window and try again. I guess there is value for localStorage.getItem('cartItems') here:

store.js

const cartItemsFromStorage = localStorage.getItem('cartItems')
  ? JSON.parse(localStorage.getItem('cartItems'))
  : []
talmax1124 commented 3 years ago

@basir I have tried cloning the repo but it gives me a error and I set up the repo's and everything. Is there another command or something I'm doing wrong?

basir commented 3 years ago

What error do you get? Share screenshot please

On Mon, Nov 9, 2020 at 7:31 PM Carlos notifications@github.com wrote:

@basir https://github.com/basir I have tried cloning the repo but it gives me a error and I set up the repo's and everything. Is there another command or something I'm doing wrong?

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/bradtraversy/proshop_mern/issues/60#issuecomment-724105703, or unsubscribe https://github.com/notifications/unsubscribe-auth/AATUFQZNDBAAOPG6KRSD6CTSPAG6NANCNFSM4TOXRSAA .

NickSchmitt commented 3 years ago

In incognito browser, Storage.cartItems is correctly an array of objects:

cartItems: "[{"product":"5fa8a16...etc

In my browser, Storage.cartItems is incorrectly an object of objects:

cartItems: "{"item":{"product":"5fa34c1e

Is this a bug in my project code's localStorage that has persisted over to your finished repo's localStorage?

I deleted the bug from local storage in the Chrome dev tools, and now it is functioning correctly. Thank you for the assistance.

ShwetaSachan commented 1 year ago

TypeError: cartItems.map is not a function

ShwetaSachan commented 1 year ago

I cloned your finished repo, and it's fully functioning except I receive the following error when trying to access the cart.

TypeError: cartItems.map is not a function
CartScreen
src/screens/CartScreen.js:41
  38 |     Your cart is empty <Link to='/'>Go Back</Link>
  39 |   </Message>
  40 | ) : (
> 41 |   <ListGroup variant='flush'>
     | ^  42 |     {cartItems.map((item) => (
  43 |       <ListGroup.Item key={item.product}>
  44 |         <Row>

All I have done is npm install and add my Mongo URI to .env.

Any idea why I might be running into this error?

TypeError: cartItems.map is not a function

ShwetaSachan commented 1 year ago

TypeError: cartItems.map is not a function