2

Recently I wanted to release a new patch version of my package and I decided to check something on the testing trial production.

To my great surprize, there was some error which I didn't remember in the released version of my package and I didn't expect.

The error was saying:

Cannot read properties of undefined (reading 'copy')

Cannot read properties of undefined (reading 'copy')

1 Answer 1

2

The problem is that utility component doesn't support reference through this in Lightning Web Security mode while it allows it when Lightning Web Security is disabled.

When you have a component Utils with the following code

export function x1(source){
    return source && source.length;
}

export function x2(source) {
    return this.x1(source) + 1;
}

and another component Main

import {x2} from 'c/utils';

export default class Main extends LightningElement {
    connectedCallback() {
        alert(x2([]));
    }
}

and you open component Main you will receive error Cannot read properties of undefined (reading 'x1') in Lightning Web Security mode and no error when Lightning Web Security is disabled.

To fix this problem, you need to remove this reference from the utility component

export function x1(source){
    return source && source.length;
}

export function x2(source) {
    return x1(source) + 1;
}

You must log in to answer this question.

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