[SalesForce] action.setCallBack() – call Back failed in lightning application

<!----setCallBackCheck.cmp-->
<aura:component controller="LtngServerSideController">
    <aura:attribute name="accountName" type="String"/>
    <ui:outputText value="{!v.accountName}"/>
    <ui:button press="{!c.getName}" label="Get A Name"/>
</aura:component>

/*--------setCallBackCheckController.js------*/
({
    getName : function(component, event, helper) {
        var action = component.get("c.getAccountName");
        $A.enqueueAction(action);
        action.setCallback(this,function(response)){
            var status = response.getStatus();
            if(status === "SUCCESS"){
                component.set("v.accountName", response.getReturnValue());
            }
        }
})

/*--------setCallBackCheckController.apxc------*/
public class LtngServerSideController {
    @AuraEnabled
    public static string getAccountName(){
        List<account> accounts = [SELECT Id, Name FROM Account LIMIT 1];
        return accounts[0].Name;
    }
}

enter image description here

Best Answer

There are some issues with the client-side javascript controller that you wrote for your component.

Here is the corrected code:

({
    getName : function(component, event, helper) {
        var action = component.get("c.getAccountName");
        // your code as extra closing in the following line as well
        action.setCallback(this,function(response){
            // you should be using getState on result as getStatus doesn't exist in standard API
            var status = response.getState();
            if(status === "SUCCESS"){
                component.set("v.accountName", response.getReturnValue());
            }
        // your setCallback wasn't closed properly
        });
        // you enqueue the action even before it was setup
        $A.enqueueAction(action);
    }
})

I would recommend going through documentation on how to call apex methods from lightning component controllers.

Hope this helps.

Related Topic