2

I cannot seem to get the fields from force:recordData available to my component. I'm fairly confident I have the syntax wrong, but I can't find the proper way to reference the data.

This code in the component works perfectly:

<force:recordData aura:id="record"
                  layoutType="FULL"
                  recordId="{!v.recordId}"
                  targetError="{!v.recordError}"
                  targetRecord="{!v.record}"
                  targetFields="{!v.simpleRecord}" />

<!--init is triggered on render and sets up columns and gets record data-->
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />

<!-- Display a lightning card with details about the record -->
<div class="Record Details"> 
    <lightning:card iconName="standard:account" title="{!v.simpleRecord.Name}" >
        <div class="slds-p-horizontal--small">
            <p class="slds-text-heading--small">
                <lightning:formattedText title="Invention" value="{!v.simpleRecord.Invention__c}" /></p>
            <p class="slds-text-heading--small">
                 <lightning:formattedText title="Record Type" value="{!v.simpleRecord.RecordTypeId}" /></p>
        </div>
    </lightning:card>
</div>

I can get it to display the values that are obviously in the fields. However, I cannot get access to that same field in the js controller.

doInit : function(component, event, helper) {
    //get record type of initial record
    var passRecID = component.get("{!v.simpleRecord.RecordTypeId}");
    console.log('The recordID pulled is ' + passRecID);

When I do that code, I get this error:

The recordID pulled is null

5
  • I have also tried just component.get("v.simpleRecord.RecordTypeId");. I just threw the brackets in there to make sure I wasn't missing them. I forgot to remove them for the post.
    – Chance
    Commented Mar 13, 2018 at 19:52
  • Is you component using "force:hasRecordId" interface? Commented Mar 13, 2018 at 20:17
  • @PranayJaiswal yes, I have that in the component. I am sorry I didn't include it.
    – Chance
    Commented Mar 13, 2018 at 20:19
  • Can you provide full code? And how are testing this component? But putting it in Lightning app builder or using Lightning Application for testing? Commented Mar 13, 2018 at 20:20
  • @PranayJaiswal That is basically all the code. There isn't really anything else to go off of. I am testing it by adding the component to a record page and opening up developer tools in Chrome. The important part is console.log('The recordID pulled is ' + passRecID);
    – Chance
    Commented Mar 13, 2018 at 20:23

1 Answer 1

4

I dont know if its a bug or Lightning Application Lifecyle, but Init event is called first even before the records are loaded with force:recordData.

You can test this behaviour by adding a button in your code and see same JS controller method returning you the required value.

<lightning:button variant="brand" label="Submit" onclick="{! c.doInit }" ></lightning:button>

Solution: It looks like you cannot use doInit event's functionality. There is a workaround for it. force:recordData has a recordUpdated method which is called when the record is initialized/changed. You can take advantage of this to call your logic instead of doInit.

Your code will now look like.

<force:recordData aura:id="record"
    layoutType="FULL"
    recordId="{!v.recordId}"
    targetError="{!v.recordError}"
    targetRecord="{!v.record}"
    targetFields="{!v.simpleRecord}"
    recordUpdated="{!c.doInit}"
/>

<!--Commenting Do init method.-->
<!--<aura:handler name="init" value="{!this}" action="{!c.doInit}" />-->   
1
  • 3
    It's not a bug. If init got hung up by a trip to the server, it'd cause massive UI delays.
    – sfdcfox
    Commented Mar 13, 2018 at 22:20

You must log in to answer this question.

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