Skip to content
Advertisement

Cannot assign to read only property ‘winner’ of object ‘[object Object]’

I’m encountering the error

JavaScript

when I try to make the assignment

this.contest.winner = winnerId;

you see in the following code snippet:

JavaScript

I’ve done this kind of assignment with other classes around the project and they never bothered me.

I enclose the contest class and post if it is useful:

JavaScript

I also tried some solutions always found on this forum such as:

Object.assign(target, source);

But he rightly tells me that this.contest.winner is null or undefined.

I hope for your help. Thank you

Edit:

This is the entire component.ts where onWinner() is present.

JavaScript

Advertisement

Answer

contest contains a reference to an Observable emission, and all references emitted by Observables are readonly. Either clone contest:

JavaScript

Or, better, leave it as an Observable and consume it as such, generally via the async pipe. Also, if you’re using NgRx, you want to be using store.select():

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement