Lwc: Mutating @api array results in a confusing error message

Created on 25 Feb 2020  路  5Comments  路  Source: salesforce/lwc

Description

Steps to Reproduce

https://developer.salesforce.com/docs/component-library/tools/playground/IaX_i6WN/3/edit

export default class Child extends LightningElement {
    @api
    objects = [];

    connectedCallback() {
        this.objects.push('child mutated label');
    }
}

Actual Result:
_Error: Invalid mutation: Cannot set "0" on "". "" is read-only._

Expected:
_Error: Invalid operation. The 'objects' property is read-only._

BUG P3 bug

Most helpful comment

@pmdartus i find the Cannot set "0" part to be very confusing as it may not be obvious to a novice user that such message is related to array.push operation. What if we use:
_"Invalid operation. @api properties are read-only and cannot be mutated."_

All 5 comments

This issue has been linked to a new work item: W-7262124

@apapko The error message can't contain the mutate object name since the object name is bound to the current context scope. This information is not available from the ReadOnly proxy.

The best thing we can do here is to print the original target type instead of coercing the original target name to string.

Error: Invalid mutation: Cannot set "0" on a read-only object.

@pmdartus i find the Cannot set "0" part to be very confusing as it may not be obvious to a novice user that such message is related to array.push operation. What if we use:
_"Invalid operation. @api properties are read-only and cannot be mutated."_

We will need to open a new API on the ReadOnlyProxy to parametrize the error message.

I might be able to help here, we do similar stuff in the membrane for locker. Let's chat about it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rgalanakis picture rgalanakis  路  5Comments

priandsf picture priandsf  路  5Comments

ekashida picture ekashida  路  4Comments

jodarove picture jodarove  路  5Comments

pmdartus picture pmdartus  路  4Comments