0

Trying to use url in lightning-datatable but getting this error: [Cannot add property recordDetailUrl, object is not extensible]

JS:

{
    label: ' Name',
    fieldName: 'recordDetailUrl',
    initialWidth: 200,
    type: 'url',
    typeAttributes: { label: { fieldName: 'name' } },
    sortable: true
},

async connectedCallback(){
    const act = await getAllData();

    for (const acti of act) {
        acti.recordDetailUrl = await this.getRecordUrl(acti.id);
    }
    this.List = act;
}
async getRecordUrl(recordId) {
    const recordReference = {
        type: 'standard__recordPage',
        attributes: {
            recordId,
            objectApiName: XXXXXXXX.objectApiName,
            actionName: 'view'
        }
    };

    const url = await this[NavigationMixin.GenerateUrl](recordReference);
    return url;
}

1 Answer 1

3

Anything you get from an Apex call or wire method is marked as "read-only" in order to not corrupt the cache. To actually modify something, you need to make a copy of it first:

const act = [...(await getAllData())].map(acti => ({...acti})); // spread object copy
for(const acti of act) {
  acti.recordDetailUrl = await this.getRecordUrl(acti.id);
}

Note we're also making a copy of the original array (via Array.prototype.map).

4
  • Thanks , I tried updating the code but now I'm getting error as [b.default(...).map is not a function] Commented Feb 9, 2021 at 12:19
  • You can replace const act = (await getAllData()).map(acti => ({...acti})); with const act = [...(await getAllData())];. It'll work just fine.
    – Jay Rathod
    Commented Feb 9, 2021 at 12:45
  • @JayRathod Thanks for that. I didn't actually have a chance to test this code, but that looks ... almost right? The individual indexes would still be read-only, wouldn't they?
    – sfdcfox
    Commented Feb 9, 2021 at 13:08
  • Oops. My bad. Spread operator only does shallow copy hence individual indexes will still be read-only. @sfdcfox Thank you for pointing out. You're really an SF Guru. Hi Salesforce Developer, please ignore my last comment.
    – Jay Rathod
    Commented Feb 9, 2021 at 13:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .