In Salesforce, when creating and deploying an LWC component to a Lightning page, it might return an error: “Cannot read properties of undefined (reading ‘data’).” This error generally occurs when we try to deploy the component with undefined access or improperly initialized properties.
We might not get any errors in the code while creating and deploying the source code to the Salesforce org. It only happens while adding the LWC component on the Lightning page via the Lightning app builder.
In this Salesforce tutorial, we will view and analyse the code that returned the error “Cannot read the properties,” and then we will fix it.
Resolve LWC component connect phase error: Cannot read properties of undefined.
In my case, I got the error while I set a parent-child communication component setup where the child component emits a custom event, and the parent component listens to it. While adding a component on a Lightning app page, it returned the error “Cannot read properties of undefined (reading ‘indexOf’).”
Let’s see the code from the LWC components that were returning an error.
Child Component Javascript:
import { LightningElement } from 'lwc';
export default class ChildComponent extends LightningElement {
productOptions = [
{ label: 'Laptop', value: 'Laptop' },
{ label: 'Smartphone', value: 'Smartphone' },
{ label: 'Tablet', value: 'Tablet' },
{ label: 'Smartwatch', value: 'Smartwatch' }
];
handleProductChange(event) {
const selectedProduct = event.detail.value;
console.log('Selected Product:', selectedProduct);
});
Parent Component JavaScript:
The code below in the JS file of the Parent component was returning the error.
import { LightningElement, track } from 'lwc';
export default class ParentComponent extends LightningElement {
@track selectedProducts;
handleProductSelect(event) {
this.selectedProducts = event.detail.data.join(', ');
}
}
Returned error:

Now, let’s analyse why it returned the connection phase error for the Parent JS file.
In the @track selectedProducts, no value is assigned to select products. After this, the “event.detail.data” is undefined, causing.join(‘, ‘) to fail and cause the error. So, we can see that the event is not structured correctly, and the component might try to render before the selected products are initialised.
Fixing the LWC component connect phase error:
1. First, we will fix the event handling in the child component with the correct code structure.
handleProductChange(event) {
const selectedProduct = event.detail.value;
console.log('Selected Product:', selectedProduct);
const productEvent = new CustomEvent('productselected', {
detail: selectedProduct,
bubbles: true,
composed: true
});
this.dispatchEvent(productEvent);
}
In the event dispatching, we have introduced a custom event named ‘productselected’, allowing other components to listen to this event.
We have also implemented the Bubbling and Composition feature. When the execute “