1

I have this code that must call a child component based on the profile of the user. If 'isProfileA' attribute is true, it must call 'componentA', otherwise call 'componentB'. In my case, 'isProfileA' is true however it calls 'componentB' first and then 'componentA'. The goal is just to call 'componentA'. I don't know why it still enters the else statement. The value of 'isProfileA' is set to true on init. I have also debugged the Apex and JS and its value is true. Does anyone have an idea what could be the problem?

This is the cmp.

<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="isProfileA" type="Boolean" default="false"/>


<aura:if isTrue="{!v.isProfileA}">
    <c:ComponentA/>
    <aura:set attribute="else">
        <c:ComponentB/>
    </aura:set>
</aura:if>  

This is the controller.js.

({
    doInit : function(component, event, helper) {
        let action = component.get('c.checkIfProfileA');
        action.setCallback(this, function(response){
           let state = response.getState();
            if(state == "SUCCESS") {
                let returnValue = response.getReturnValue();
                component.set('v.isProfileA', returnValue);
            }
        });
        $A.enqueueAction(action);
    }
})
4
  • If you add an alert in JS after enqueAction method - alert(component.get('v.isProfileA')); are you getting correct value? Commented Mar 3, 2023 at 12:51
  • Hi, I tried adding it. The value I'm getting after enqueAction is False. I can't find why the condition 'else' is already getting triggered. After that, it will go on the setting up of v.isProfileA to true and will then trigger the if condition.
    – Mareux
    Commented Mar 3, 2023 at 15:21
  • i cannot find any relevant doc to support this but looks like aura:if is getting executed beofre callback is complete. maybe you should also check if callback is complete or not by one more boolean default to false and make it true inside callback. change aura if to ceck both profile boolean as weel as callback is complted or not Commented Mar 4, 2023 at 16:34
  • 1
    General reminder: Aura is very obsolete. If you are not working in one of the few remaining areas that requires Aura, you will have a much better time with Lightning Web Components. LWCs allow you to use async/await, which makes order of execution much clearer.
    – David Reed
    Commented Mar 5, 2023 at 20:54

1 Answer 1

2

Calling a server method is always an asynchronous operation. That means that the default value will be shown for at least the amount of time it takes for the server-side code to return a value.

Let's look at your code, with some comments:

({
    doInit : function(component, event, helper) {
        // Create an action
        // This line runs first.
        let action = component.get('c.checkIfProfileA');
        // Set a handler to call after the call returns
        // This line runs second.
        action.setCallback(this, function(response){
           // This line runs fourth.
           let state = response.getState();
           // This line runs fifth.
            if(state == "SUCCESS") {
                // This line runs sixth.
                let returnValue = response.getReturnValue();
                // This will be the SECOND time we render the state
                // This line runs seventh.
                component.set('v.isProfileA', returnValue);
            }
        });
        // Send the request to the server
        // This line runs third.
        $A.enqueueAction(action);
        // End of init; this is the FIRST time we render the state
    }
})

Instead, consider showing a spinner:

<aura:attribute name="isProfileA" type="Boolean" default="{!false}"/>
<aura:attribute name="isLoading" type="Boolean" default="{!true}"/>

<aura:if isTrue="{!v.isLoading}">
    <lightning:spinner />
    <aura:set attribute="else">
        <aura:if isTrue="{!v.isProfileA}">
            <c:ComponentA/>
            <aura:set attribute="else">
                <c:ComponentB/>
            </aura:set>
        </aura:if>  
    </aura:set>  
</aura:if>

Remember, asynchronous code doesn't run in source code order, but rather based on asynchronous events.

1
  • Thank you for the explanation. I have also tried adding a spinner, and it already works as expected. Thanks.
    – Mareux
    Commented Mar 13, 2023 at 7:18

You must log in to answer this question.

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