Cannot read properties of undefined (reading ‘includes’)

lightning-web-components

With this code I trie to verify if the field Code__c that is a string , if the field contains the value 'BC' the Visibily should be false but when I trie this code I get this error .

Uncaught (in promise) TypeError: LWC component's @wire target property or method threw an error during value provisioning. Original error:
[Cannot read properties of undefined (reading 'includes')]

My Code :

import FIELD from '@salesforce/schema/MyContact__c.Account__r.Code__c';
import {getRecord,getFieldValue} from 'lightning/uiRecordApi';
  @track Visibily = false;
    @track value;
    @wire(getRecord, { recordId: '$recordId', fields: [FIELD] })
    dippingN({ data, error }) {
        if (data) {
            this.value == data.fields.Account__r.Code__c;
            if (this.value.includes('BC')) {
                this.Visibily = false;
            }else{
                this.Visibily = true;
            }
        }
    };

Best Answer

Looks like the property this.value can be undefined.

Add a Null/undefined check to your code to make it not fail

@track Visibily = false;
@track value;
@wire(getRecord, { recordId: '$recordId', fields: [FIELD] })
dippingN({ data, error }) {
    if (data) {
        this.value == data.fields.Account__r.Code__c;
        if(this.value) {
          if (this.value.includes('BC')) {
            this.Visibily = false;
        } else {
            this.Visibily = true;
        }
      } else {
         this.Visibily = true;
       }

    }
};