# Function cannot read property but other function can?

**URL:** <https://forum.freecodecamp.org/t/function-cannot-read-property-but-other-function-can/384359>\
**Category:** General\
**Created:** [May 6, 2020, 12:09am UTC](https://forum.freecodecamp.org/t/function-cannot-read-property-but-other-function-can/384359 "2020-05-06T00:09:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Imstupidpleasehelp](https://avatars.discourse-cdn.com/v4/letter/i/7ab992/32.png) [@Imstupidpleasehelp](https://forum.freecodecamp.org/u/Imstupidpleasehelp)\
**Post date:** [May 6, 2020, 12:09am UTC](https://forum.freecodecamp.org/t/function-cannot-read-property-but-other-function-can/384359/1 "2020-05-06T00:09:30Z")

</div>

Hello everyone, I am having a strange issue.  
I have this function that works fine. And I want to write a function that does the opposite.

```auto
addToCart(e) {
    this.setState({
      cart: [...this.state.cart, [e.target.value, e.target.name, e.target.id]],
    });

```

So I wrote this function, but for some reason this one is telling me that “TypeError: Cannot read property ‘cart’ of undefined”  
I don’t understand why this function is not working but the addtocart() is working just fine.

```auto
Removefromcart(e) {
    this.setState({
      cart: [
        this.state.cart.splice([e.target.value, e.target.name, e.target.id]),
      ],
    });

```

Thanks in advance for your expertise.  
My full code is here.

> **[distracted-fire-ctc3c - CodeSandbox](https://codesandbox.io/s/distracted-fire-ctc3c)**
>
> distracted-fire-ctc3c by Imstupidpleasehelp using @testing-library/jest-dom, @testing-library/react, @testing-library/user-event, nuka-carousel, react, react-async-script-loader, react-dom, react-paypal-express-checkout, react-redux

---

<div class="post-metadata">

**Author:** ![i-tech](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/i-tech/32/141825_2.png) [@i-tech](https://forum.freecodecamp.org/u/i-tech)\
**Post date:** [May 6, 2020, 12:48am UTC](https://forum.freecodecamp.org/t/function-cannot-read-property-but-other-function-can/384359/2 "2020-05-06T00:48:18Z")

</div>

I see 2 opening curly braces but only closing one. Well , for me its kinda advanced JS . If you are hurry, post it on stackoverflow, you will receive your awaiting answer in few seconds.

---

<div class="post-metadata">

**Author:** ![lasjorg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lasjorg/32/280761_2.png) [@lasjorg](https://forum.freecodecamp.org/u/lasjorg)\
**Post date:** [May 6, 2020, 1:09am UTC](https://forum.freecodecamp.org/t/function-cannot-read-property-but-other-function-can/384359/3 "2020-05-06T01:09:54Z")

</div>

1. You didn’t bind the method. I’d suggest just using arrow syntax instead.

```auto
Removefromcart = (e) => {
...code
}

```

1. You are trying to mutate the cart array in the state, that is a big no-no.

2. That is not how [splice](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) works.

3. You are not passing the event to the method.

---

<div class="post-metadata">

**Author:** ![Imstupidpleasehelp](https://avatars.discourse-cdn.com/v4/letter/i/7ab992/32.png) [@Imstupidpleasehelp](https://forum.freecodecamp.org/u/Imstupidpleasehelp)\
**Post date:** [May 6, 2020, 1:25am UTC](https://forum.freecodecamp.org/t/function-cannot-read-property-but-other-function-can/384359/4 "2020-05-06T01:25:02Z")

</div>

That’s what I call expertise!

1. So I changed it to be an arrow function.
2. I know I should not change the state directly, but what other options do I have in this case?
3. So I will read up on how to use the splice function, i can see I was wrong.
4. I thought that I was, what is the (e) for then?  
Thank you again for your help. You are like my personal mentor. (along with all of FCC)

---

<div class="post-metadata">

**Author:** ![lasjorg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lasjorg/32/280761_2.png) [@lasjorg](https://forum.freecodecamp.org/u/lasjorg)\
**Post date:** [May 6, 2020, 1:27am UTC](https://forum.freecodecamp.org/t/function-cannot-read-property-but-other-function-can/384359/5 "2020-05-06T01:27:37Z")

</div>

I’m going to add a post to your other thread. I think it is more relevant to the questions you have.

> [@Dynamically removing from array](http://forum.freecodecamp.org/t/dynamically-removing-from-array/383878/11):
>
> You should generate a unique id for each product, use something like the [uuid package](https://www.npmjs.com/package/uuid). Pass the id to the Removefromcart method and filter the state using the id. You should really make the cart an array of objects, not an array of arrays. That way you can reference each property by name and not by index. Instead of passing the event to addCart and constructing the state from it, pass the serviceitems object and get the properties that you already have from the JSON data. Just like you a…

---

<div class="post-metadata">

**Author:** ![lasjorg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lasjorg/32/280761_2.png) [@lasjorg](https://forum.freecodecamp.org/u/lasjorg)\
**Post date:** [May 6, 2020, 1:33am UTC](https://forum.freecodecamp.org/t/function-cannot-read-property-but-other-function-can/384359/6 "2020-05-06T01:33:31Z")

</div>

> [@Imstupidpleasehelp](#):
>
> I thought that I was, what is the (e) for then

The method accepts a parameter, yes. But you didn’t pass in the event as an argument when calling the method.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:55pm UTC](https://forum.freecodecamp.org/t/function-cannot-read-property-but-other-function-can/384359/7 "2021-06-01T18:55:40Z")

</div>

Closed on June 1, 2021, 6:55pm UTC


