Uncaught TypeError: Can't add property 12, object is not extensible

后端 未结 1 1474
迷失自我
迷失自我 2021-01-05 16:54

I can\'t seem to understand the error I am getting on my client application. I am subscribing to a graphql subscription and I am able to retrieve the updates but I am not be

相关标签:
1条回答
  • 2021-01-05 17:36

    I suppose this.models is an array returned by Apollo and you want to add new created object to your initial array ? If true, Apollo returns an immutable Object !

    You have to clone the initial returned array. Something like in the subscribe function:

    this.apollo
        .watchQuery({query: INITIAL_GQL_REQUEST})
        .subscribe((data) => {
            this.models = data.models.map((model) => {
                return {
                    id: model.id, 
                    name: model.name,
                    another: model.another
                }
            })
        };
    });
    

    Then your subscription request will be able to add a created model to this plain javascript array.

    PS: Not sure but I suppose Apollo returns immutable objects because they are stored in the store and depending on your fetch policy, it can miss store hits if your are able to mutate them.

    Hope it helps

    0 讨论(0)
提交回复
热议问题