0

I am getting array of string from component's attribute to lightning controller, now I want to split that string to comma separated values and want to assign that to another component variable but split function is not working in js controller, the below error I am getting:

Action failed: c:ContactCardInfo$controller$doInit [Cannot read property 'split' of undefined]`

controller.js

({
    doInit: function(component, event, helper) {
        console.log('FieldApiNames '+component.get('v.FieldApiName'));
        var fieldString = component.get('v.FieldApiName');
        var array = [];
        array = fieldString.split(',');
        component.set('v.conFields', array);
        helper.getContactRecordId(component);
    }

})

Component:

<aura:component implements="force:appHostable,force:hasSObjectName,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" controller="ContactInfoController">

<aura:handler name="init" value="{!this}" action="{!c.doInit}" />

<aura:attribute name="contactLookup" type="string"/>
<aura:attribute name="HeaderLabel" type="string"/>
<aura:attribute name="FieldApiName" type="string"/>

<aura:attribute name="contactRecordId" type="id" />
<aura:attribute name="recordId" type="String"/>
<aura:attribute name="sobjecttype" type="String" default="{!v.sObjectName}"/>
<aura:attribute name="conFields" type="string[]"/>

<aura:attribute name="HeaderTitle" type="Aura.Component[]">
    <h2>
        <b>{!v.HeaderLabel}</b>
    </h2>
</aura:attribute>

<lightning:card iconName="standard:contact" title="{!v.HeaderTitle}" />
<lightning:recordForm aura:id="recordViewForm"
                      objectApiName="Contact"
                      columns="2"
                      recordId="{!v.contactRecordId}"
                      fields= "{!v.conFields}"
                      mode="View" />

Design

<design:component label="Contact Object Configuration">
<design:attribute name="HeaderLabel" label="Header Label" />
<design:attribute name="contactLookup" label="Contact Field API Name" description="API Name of the Contact Object"/>
<design:attribute name="FieldApiName" label="Field API Name" />

5
  • what do you have in FieldApiName attribute? can you provide component markup as well Jan 18, 2019 at 11:50
  • 'Name','Email' there are the values in the console log
    – user45297
    Jan 18, 2019 at 11:52
  • what type of FieldApiName attribute is? Jan 18, 2019 at 12:01
  • I have updated my comments with Component and design as well, hope this help, btw i am taking FieldApiName from design attribute
    – user45297
    Jan 18, 2019 at 12:09
  • I added, but looks like my controller class is keep getting called, now I am getting too many soql, in the console log also the log is keep getting printed.
    – user45297
    Jan 18, 2019 at 12:20

2 Answers 2

1

As I can see from the code in markup the <aura:attribute name="FieldApiName" type="string"/> , you haven't specified the default value of attribute 'FieldApiName'. Thus its undefined. So in your Javascript var fieldString = component.get('v.FieldApiName'); is null/undefined . You cannot call split method on null/undefined.

Pass the FieldApiName from the App Builder or lightning Application you are using.

or in your markup default it to

<aura:attribute name="FieldApiName" type="string" default="Id"/>
6
  • in these 2 below lines I am getting the values in console log, so v.FieldAPIName is having values definitely: console.log('FieldApiNames '+component.get('v.FieldApiName')); var fieldString = component.get('v.FieldApiName');
    – user45297
    Jan 18, 2019 at 12:27
  • i am getting console.log('FieldApiNames '+component.get('v.FieldApiName')); printing undefined. @Nshukla6 Jan 18, 2019 at 12:33
  • this FieldAPIName, I am taking from Lightning page via Design attribute..
    – user45297
    Jan 18, 2019 at 12:42
  • Is it breaking in AppBuilder? or in page? Jan 18, 2019 at 12:43
  • Also can you try adding default as <aura:attribute name="FieldApiName" type="string" default="Id" /> Jan 18, 2019 at 12:44
0

I solved my problem by configuring FieldAPIName design attribute as Name,Email instead of 'Name','Email'. We cannot have the ' character in the string.

You must log in to answer this question.

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