1

I have the following LWC, when I try to add on my lightning record page got some error like, cannot read property of split.

enter image description here

js:

import { LightningElement, api, track } from 'lwc';
    export default class CustomRecordDetail extends LightningElement {
     @track fieldArray;
        connectedCallback()
         {
            if(this.fields !== null && typeof this.fields !== undefined && this.fields !== '')
            {
                this.fieldArray = this.fields.split(",");
            }
    }
    }

html :

<template>
    <div class="slds-card">
        <div class="slds-grid slds-page-header forceRelatedListCardHeader">
            <header class="slds-media slds-media_center slds-has-flexi-truncate">
                <div class="slds-media__figure"> 
                     <lightning-icon icon-name={icon} size={size} alternative-text="Connected">
                    </lightning-icon>     
                </div>
                <div class="slds-media__body">
                    <h2 class="slds-card__header-title slds-text-title_bold"> {title} </h2>
                </div>
            </header>
        </div>
       
                <div class="slds-media__body">
                    <div class="slds-p-left_medium">

                    <lightning-record-form
                    record-id={recordId}
                    fields={fieldArray}>
                    </lightning-record-form>
                </div>
                </div>
       </div>

xml:

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>50.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__RecordPage</target>
    </targets>
        <targetConfig targets="lightning__RecordPage">
            <property name="fields" type="string"/>
            </targetConfig>
    </targetConfigs>
</LightningComponentBundle>
1
  • 2
    Where is this.fields defined/initialized? It is used in the connectedCallback. I see you have a tracked property, fieldArray but this would be referenced as this.fieldArray not this.fields.
    – Phil W
    Dec 16 '20 at 10:57
0

I think what you really want should be achievable by just

if(this.fields) {
   this.fieldArray = this.fields.split(",");
}

Can you give it a try and let us know if you stop receiving the error?

0
@track fieldArray=[]; // Array to store a value
connectedCallback() {
    if(this.fields !== null && typeof this.fields !== undefined && this.fields !== ''){
        this.fieldArray = this.fields.split(",");
    }
1
  • Is this really an answer to your question, or is it something you meant to add to your question by making an edit?
    – Derek F
    Dec 16 '20 at 15:00
0

When you use typeof in JS. That returns a string value. Documentation

So if you check typeof this.fields !== "undefined", then it should be fine.

0

import { LightningElement, api, track } from 'lwc'; export default class CustomRecordDetail extends LightningElement { @track fieldArray; @track fields="How are you doing today?"; connectedCallback() { if(!this.fields) { this.fieldArray = this.fields.split(" "); } } }

0

I see that you have not declared the variable 'fields' in your js file on which you are performing the 'split' operation, Please declare and initialize the variable and then try.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.