I have a component which is a form page and one of its property is the following:
profile: Profile;
When the component load up, in the ngOnInit function I grab the profile I have inside my ngRx store and I copy it into the profile object variable I have defined above:
this.store.pipe(
take(1),
select(user)
).subscribe(myUser => {
this.profile = {...myUser};
this.profile.details.firstname = "John";
// ... more setup code
When I try to assign the string "John" to the object I copied it fails with the error:
TypeError: Cannot assign to read only property 'firstName' of object '[object Object]'
Why does this happen? How can I solve it?
My idea is to copy the object I have inside the store, to modify it with a 2-way-binding using ngModel when the user compiles my form with all the data I need (email, first name, etc) and save the new object inside the store when the user hits the submit button (dispatching an action this time, as it should be).
This question says that the spread operator should do the job so that I can work with a copy and pass the copy as a payload when dispatching the action, but in my case it doesn't work and I don't understand why.
profileInfo? - Rahul SharmaprofileInfo, it'sprofile. I'm gonna correct it. - RexamcloneDeepas @Tiep Phan points out. - kalexi