Why are SObject fields returned to an LWC via @AuraEnabled Apex immutable?

I get this error:

TypeError: ‘set’ on proxy: trap returned falsish for property
‘cvcp__ReturnToWorkType__c’

when attempting to set a value via JavaScript inside an LWC (Lightning Web Component):

sob.cvcp__ReturnToWorkType__c = 'My Type';

where the SObject has been returned from an explicitly called @AuraEnabled method.

Now I get that immutability is often a good thing, and I get that if the data was managed by a service that could feed any changed (in the database) values through to clients it could make sense to not allow such updates.

But why the immutability in this case (that is not the same as Uncaught TypeError: ‘set’ on proxy: trap returned falsish for property Name as there is no caching)?

(This matters because modifying the data can be a clean way to go for some features. So an option to turn the immutability off where appropriate would be a good thing IMHO.)

Answer

The objects returned by AuraEnabled Apex methods are immutable when they are annotated with (cacheble=true), since it is to be cached on the browser, it will return the same copy of object wherever the apex method is invoked within LWC, this implies it should be readonly to avoid issues.

When the methods are not annotated with (cacheble=true), the values will not be cached and hence are not immutable.

Attribution
Source : Link , Question Author : Keith C , Answer Author : Prashant Kashyap

Leave a Comment