Infinite Loop Caused by Controller Action in Lightning Component

the following code is causing an infinite loop in my lightning component when I click the ui:button. Checking debug logs, nothing ever goes to the apex controller.

.cmp layout

<aura:attribute name="account" type="Account" default="{ 'sobjectType': 'Account'}"/>
<aura:attribute name="deliveryMethod" type="string[]" />
<ui:inputSelect value="{!v.account.Delivery_Method__c}" multiple="true">
  <aura:iteration items="{!v.deliveryMethod}" var="p">
    <ui:inputSelectOption text="{!p}" label="{!p}"/>
  </aura:iteration>
</ui:inputSelect>
<div>
 <ui:button label="Update Preferences" press="{!c.saveAccount}" />
</div>

js controller:

saveAccount : function(component,event,helper){
    var a = component.get("v.account");
    var saveAction = component.get("c.saveAccount");
    saveAction.setParams({
        acct : a
    })
    saveAction.setCallback(this,function(res){
        console.log('callback');
    });
    $A.enqueueAction(saveAction);
}

and my Apex Controller:

@AuraEnabled
public static ResponseDto saveAccount(Account acct){
    try{
        upsert acct;
    }catch(Exception e){
        ResponseDto res = new ResponseDto(false, 'System Error has occurred:' + e.getMessage());
        return res;
    }
    // Create Object for response as Success Completion. Insert the return value to Map
    ResponseDto res = new ResponseDto(true, '');
    return res;
}

public class ResponseDto {
    @AuraEnabled public Boolean isSuccess { get; set; }
    @AuraEnabled public String message { get; set; }
    @AuraEnabled public Map<Object, Object> values { get; set; }
    public ResponseDto(Boolean isSuccess, String msg){
        this.isSuccess = isSuccess;
        this.message = msg;
        this.values = new Map<Object, Object>();
    }
}

Answer

I found the solution. DO NOT GIVE YOUR JAVASCRIPT METHOD AND YOUR APEX CONTROLLER METHOD THE SAME NAME. When I defined the saveAction:

var saveAction = component.get("c.saveAccount");

the saveAction was set as the js controller’s saveAccount method, and not my Apex controller’s saveAccount method. I assumed that anytime we referenced “c.methodName” from the js controller we were referencing the Apex method – that’s not the case and this seems pretty buggy to me.

Probably should be added as a best practice or perhaps a known issue somewhere.

I changed the js method name to “saveAcct” and everything works now.

Attribution
Source : Link , Question Author : Jason Benkert , Answer Author : Jason Benkert

Leave a Comment