1

I try to set a value in my lightning component via the helper function, but it does not set the value in the component, and I get the wrong value when I try to do component.get in the controller. Here is my code:

Test.cmp

<aura:component controller="TestController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,lightning:isUrlAddressable" access="global" >

   <aura:attribute name="consoleNavigation" type="Boolean" default="false" access="global"/>

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

</aura:component>

TestController.js

({
    init : function(component, event, helper) {
        helper.isConsoleNavigation(component, event, helper);
        var consoleNavigation = component.get("v.consoleNavigation");
        console.log('consoleNavigation '+consoleNavigation);
    }
})

TestHelper.js

({ 
    isConsoleNavigation : function(component, event, helper) {
        var workspaceAPI = component.find("workspace");
        workspaceAPI.isConsoleNavigation().then(function(response){
            component.set("v.consoleNavigation",response);
            console.log('isConsoleNavigation '+response);
        })
        .catch(function(error) {
            console.log('isConsoleNavigation Error '+error);
        });
    }
})

In the helper, the value gets logged properly (true if used in the Service Cloud and on Case record page), but in the controller the initialized value does not get changed. Does that have something to do with JavaScript assigning variables first? But how would I work around that? Thanks!

2
  • 2
    The fact that you can do a .then() means that is returning a promise, which the synchronous part of your code doesn't like. try returning a new Promise on the helper and putting your rest of the code of the controller in a .then May 11, 2020 at 14:07
  • Thanks, @SergioAlcocer, that was the right hint. May 12, 2020 at 7:25

1 Answer 1

2

As Sergio commented, you need to wait for the result, as it is asynchronous. This means the result won't be available until sometime later. You can fix this by using another promise:

({ 
    isConsoleNavigation : function(component, event, helper) {
      return new Promise($A.getCallback(function(resolve, reject) {
        var workspaceAPI = component.find("workspace");
        workspaceAPI.isConsoleNavigation().then(function(response){
            component.set("v.consoleNavigation",response);
            resolve(response);
        })
        .catch(function(error) {
            console.log('isConsoleNavigation Error '+error);
            reject(error);
        });
      }));
    }
})

({
    init : function(component, event, helper) {
        helper.isConsoleNavigation(component, event, helper)
        .then(function(result) {
            var consoleNavigation = component.get("v.consoleNavigation");
            console.log('consoleNavigation '+consoleNavigation);
        })
        .catch(function(error) {
            console.log(error);
        });
    }
})
1

You must log in to answer this question.

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