1

After action.setCallback code not executing, In Console log I can see only this

enter image description here

The system debug is returning in the debugging. So, the apex is being called, returning a value. No error in Apex side

someone can help me?

<aura:component controller="UserController">
    <aura:attribute name="accessLevel" type="String" />

    <!-- Fetch access level in the init handler -->
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    
    <aura:if isTrue="{!AND((v.accessLevel =='Admin'),(v.accessLevel == 'Owner'))}">
            <div class="shop-marketing slds-col slds-size_1-of-1 slds-grid slds-gutters_small slds-p-top_medium slds-p-horizontal_large slds-p-bottom_x-large">
                <div class="slds-col slds-size_1-of-3">
                    <div class="slds-col slds-size_1-of-1 slds-m-bottom_medium dashboard-title">{!$Label.c.shop}</div>
                    {!v.shop}
                </div>
                <div class="slds-col slds-size_1-of-3">
                    <div class="slds-col slds-size_1-of-1 slds-m-bottom_medium dashboard-title">Billing information</div>
                    <c:accountifo/>
                </div>
                <div class="slds-col slds-size_1-of-3">
                    <div class="slds-col slds-size_1-of-1 slds-m-bottom_medium dashboard-title">{!$Label.c.marketing}</div>
                    {!v.marketing}
                </div>
            </div>
        </aura:if>
        
        <aura:if isTrue="{!AND((v.accessLevel !='Admin'),(v.accessLevel != 'Owner'))}">
            <div class="shop-marketing slds-col slds-size_1-of-1 slds-grid slds-gutters_small slds-p-top_medium slds-p-horizontal_large slds-p-bottom_x-large">
                <div class="slds-col slds-size_1-of-2">
                    <div class="slds-col slds-size_1-of-1 slds-m-bottom_medium dashboard-title">{!$Label.c.shop}</div>
                    {!v.shop} 
                </div>
                <div class="slds-col slds-size_1-of-2">
                    <div class="slds-col slds-size_1-of-1 slds-m-bottom_medium dashboard-title">{!$Label.c.marketing}</div>
                    {!v.marketing}
                </div>
            </div>
        </aura:if>
        
</aura:component>

JS

({
    doInit : function(component, event, helper) {
         // Call the server-side action to fetch user's type
        let action = component.get("c.getUserAccessLevel");
        console.log('action access Level' +  action);
        console.log('Action Name: ' + action.getName());
        console.log('component Valid: ' +component.isValid());
        action.setCallback(this, function(response) {   
             console.log('Action Name: IN ');
            if(component.isValid()) { // Checking if the component is still valid
                console.log('State: ' + response.getState());
                if(response.getState() === "SUCCESS") {
                    // Set the user type attribute with the fetched value
                    component.set("v.accessLevel", response.getReturnValue());
                    console.log('Response: ' + response.getReturnValue());
                } else {
                    let errors = response.getError();
                    console.error("Failed to fetch access Level: " + (errors[0] && errors[0].message));
                }
            }
        });
        $A.enqueueAction(action);  
    } 
})

APEX Class

@AuraEnabled
    public static String getUserAccessLevel() {
        // Fetch the current user
        system.debug('getUserAccessLevel In -->');
        User currentUser = [SELECT Id, Contact.Access_Level__c FROM User WHERE Id =:UserInfo.getUserId()];
        system.debug('Access Level getUserAccessLevel-->' + currentUser.Contact.Access_Level__c);
        return currentUser.Contact.Access_Level__c;
    }

1 Answer 1

0

Your code currently has multiple problems. It's possible the code you're sharing isn't the code that's actually on the server.

Issue 1

In the markup, you're asking to run the init method:

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

But the controller is using doInit:

    doInit : function(component, event, helper) {

Make sure they actually agree.

Issue 2

Second, you're using and when you mean to use or:

    <aura:if isTrue="{!AND((v.accessLevel =='Admin'),(v.accessLevel == 'Owner'))}">

It is impossible for accessLevel to equal both Admin and Owner at the same time, so this can never be rendered.

Instead, you mean to use or. Also, note that JavaScript is case-sensitive, so AND isn't and, and OR isn't or. If you use the wrong one, expect problems. All expression functions are lowercase.

Also, this code can probably be optimized to eliminate redundant logic. After all, you're basically showing three columns in one condition, and two in the other. You can use expressions to simplify the template, which would also improve performance.

As a side note, there's no need to call component.isValid() in modern Aura, as callbacks only happen if the component is in a valid state.

1
  • My bad on copying the code, I corrected the code, still not working before callback console log is printing after I don't see any console log
    – SK2889
    Commented Apr 5 at 15:33

You must log in to answer this question.

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