0

I am trying to implement helper method in my lightning component but getting below error.

This page has an error. You might just need to refresh it. Action failed: sree123:calculate$controller$calculateResult [helper.calHelper is not a function] Failing descriptor: {sree123:calculate$controller$calculateResult}.

Please help me out.

Here is my component:

calculate.cmp:

<aura:component controller="AuraCalculations">
    <aura:attribute name="FirstNumber" type="decimal"/>
    <aura:attribute name="SecondNumber" type="decimal"/>
    <aura:attribute name="Result" type="decimal"/>
   <div>
       <ui:inputnumber value="{!v.FirstNumber}" label="First number"/>
       <ui:inputnumber value="{!v.SecondNumber}" label="Second number"/>
       <ui:inputnumber value="{!v.Result}" label="Result"/>
       <ui:button press="{!c.calculateResult}" label="Calculate" />
    </div>

</aura:component>

calculateController.js:

({
    calculateResult : function(component, event, helper) {
         helper.calHelper();

    }
})

Helper Method :calculateAppHelper.js:

({
    calHelper : function(component) {
        var fnumber = component.get("v.FirstNumber");
        var snumber = component.get("v.SecondNumber");
        //var result = fnumber+snumber;
        var action = component.get("c.calci");
        action.setParams({"fstNumber":fnumber,"scdNumber":snumber});
        action.setCallback(this,function(data){
            alert(data.getReturnValue());
            component.set("v.Result",data.getReturnValue());

        })
        $A.enqueueAction(action);


    }
})

Apex Class - AuraCalculations.aprx :

public class AuraCalculations {
    @AuraEnabled
    public static decimal calci(decimal fstNumber,decimal scdNumber){

        return(fstNumber+scdNumber);
    }


}
  • 1
    I suggest you edit your question and post the code with proper formatting. It's not readable that much. – the_phantom May 14 '18 at 13:16
  • While you post your formatted code, I can see in this version that you are trying to call helper.calHelper(); whereas your function is defined as calHelper : function(component) -- you are missing to pass component from your controller. – Jayant Das May 14 '18 at 13:22
  • Hi Jayant, Even after placing component also getting same error – user56612 May 14 '18 at 13:25
2

From the method definition of calHelper() needs a parameter "component". While calling the method you are not passing it and hence your helper method isnt getting called.

Solution: pass the parameter to helper.

({
    calculateResult : function(component, event, helper) {
         helper.calHelper(component);

    }
})

Code working:

enter image description here

  • Yeah i did the same but still getting same error !! – user56612 May 14 '18 at 13:24
  • @user56612 make sure you refresh/clear cache before trying to run this again – Jayant Das May 14 '18 at 13:26
  • I quickly tried your code and got a proper response, that you wanted. As @Jayant mentioned.. You need to refresh your window. I do a hard refresh using CTRL+F5, it works. – Pranay Jaiswal May 14 '18 at 13:33
  • Hi Jayant, I cleared cache and tried again but still getting same error. – user56612 May 14 '18 at 13:34
  • @PranayJaiswal, Let me check once again – user56612 May 14 '18 at 13:35
-1

You have to write helper logic in component helper(calculateHelper.js). In the above code posted by you, the logic is in calculateAppHelper.js. please move the helper logic from calculateAppHelper.js to calculateHelper.js then it should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.