1

action.setcallback doesnt get exeucted from Helper. It works fine from controller I am trying to call a server side method in apex class from lightning controller It works fine from javascript controller but code doesnt get executed from helper I want to execute code from helper because i need to call helper function based on some other event. Code from helper is pasted below.

helper.callserver(cmp,lat,lang);
({
    callserver : function(cmp,strlat,strlang) {
                    alert('inside helper callserver function call cmp is>>' + cmp +'<<');
                    var action = cmp.get("c.getLatLang");
                    alert('after getnearestvehicles call');
                    //action.setParams({"strlat":"51117320","strlang":"6789550"});
                    //alert('after setparams call');
                    action.setCallback(this, function(a){
                        alert('INSIDE SETCALLBACK ACTION MAP CLICK');
                        var strLatLang1 = a.getReturnValue();
                       alert("STR LAT LANG1 is>>>" + strLatLang1 + '<<<');
                    });
            $A.enqueueAction(action);
    }
})
4
  • Is the function in helper getting called at all? Are you getting the alerts? Feb 19, 2016 at 10:24
  • all alerts are working fine. I can see value for cmp also. control goes inside helper. code before action.setcallback gets executed properly.
    – ABHIJIT
    Feb 19, 2016 at 10:37
  • Not sure how relevant it is, but can you check if the status is SUCCESS or not inside setCallback? use if(a.getState() === "SUCCESS") Feb 19, 2016 at 11:09
  • it doesnt go inside action.setcallback hence state is not shown i had put alert on state also earlier
    – ABHIJIT
    Feb 19, 2016 at 12:20

2 Answers 2

1

Please, try below code. If this is not working propertly you should add code where you call helper method.

controller

controllerFunctionCallServer : function(component, event, helper) { 
    var strlat = '51117320';
    var strlang = '6789550';
    helper.callserver(component, strlat, strlang);  
}

helper

callserver : function(component, strlat, strlang) {

    var action = component.get("c.getLatLang");

    action.setParams({ 
        strlat : strlat,
        strlang : strlang
    });

    action.setCallback(this, function(response) {
        var state = response.getState();
        if (state === "SUCCESS") {
            alert("From server: " + response.getReturnValue());
            var strLatLang1 = response.getReturnValue();
            alert("STR LAT LANG1 is>>>" + strLatLang1 + '<<<');
        } else  {
            console.log("Calback errror!!");
        }
    });
    $A.enqueueAction(action);
}

Maybye you miss something for example 'helper' in controller. function(component, event, helper)

OR

you don't have define variable in controller strlat or strlang

OR

you have diferent problem with call helper method.

0

This is not an answer as of now.

From the documentation,

setCallback() has a third parameter that registers the action state that invokes the callback. If you don't specify the third argument for setCallback(), it defaults to registering the SUCCESS and ERROR states. To set a callback for another state, such as ABORTED, you can call setCallback() multiple times with the action state set explicitly in the third argument.

For example:

action.setCallback(this, function(response) { ...}, "ABORTED");

So its a possibility that the request is having status ABORT or INCOMPLETE. You may want to write another setCallback with the 3rd parameter and check the state of the request.

Here is the link

5
  • Even with third parameter as ABORTED it doesnt go inside action.setcallback
    – ABHIJIT
    Feb 22, 2016 at 5:34
  • I have tried with INCOMPLETE also and it doesnt go inside action.setcallback
    – ABHIJIT
    Feb 22, 2016 at 6:26
  • This callback works fine when i am doing it from javascript controller. It is not working from helper. Do we need to consider something if we have to invoke action callback from helper? Any thoughts?
    – ABHIJIT
    Feb 22, 2016 at 6:27
  • this callback doesn't work also from other javascript function defined inside javascript controller.
    – ABHIJIT
    Feb 22, 2016 at 11:10
  • has anybody seen similar behaviour before? can anybody answer this?
    – ABHIJIT
    Mar 15, 2016 at 8:32

You must log in to answer this question.

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