I want to create a page that display a list of object. I get the list of the objects from a table. then i pass it to javascript controller to display it in the lightning component. My code is below :
Controller Apex
@AuraEnabled
public static List<BatchToBeExecuted> getBatchs (){
List<BatchToBeExecuted> arrayItems = new BatchToBeExecuted[]{};
List<SICo_BatchTobeExecuted__mdt> listBatchs = [SELECT Id,DeveloperName,
BatchSize__c FROM SICo_BatchTobeExecuted__mdt ];
if(!listBatchs.isEmpty()){
for( SICo_BatchTobeExecuted__mdt aBatch : listBatchs){
arrayItems.add(newBatchToBeExecuted(aBatch.DeveloperName,aBatch.BatchSize__c));
}
}
System.debug('return value'+arrayItems);
return arrayItems ;
}
Controller Javascript
init : function(component, event, helper) {
let action = component.get("c.getBatchs");
action.setCallback(this,function(response){
const state = response.getState();
console.log('state='+state);
if(state==="SUCCESS"){
var batchs = response.getReturnValue();
var result = [{batchSize:2000, name:"SICo_DeleteArchive_Batch"}];
component.set("v.aBatchToBeExecuted", result);
console.log('return result='+JSON.stringify(batchs)) ;
}
The console.log of JSON.stringify(return value) show a blank table
<table>
<aura:iteration items="{!v.aBatchToBeExecuted}" var="batchToBeExecuted">
{!batchToBeExecuted.name}
{!batchToBeExecuted.batchSize}
</aura:iteration>
<lightning:button onclick="{!c.getString}" label="Update"/>
</table>
Best Answer
You need to call enqueueAction in your init function, and set the return value to an attribute on your component. You may find it helpful to check for the ERROR and INCOMPLETE states, and not just SUCCESS.
Include a handler for initialization.
Since you are returning an apex object instead of a custom object, you will need to annotate the variables you want to use with
@AuraEnabled
In order to get useful error messages in lightning back from apex, you will need to wrap exceptions in an
AuraHandledException
.If there aren't any errors and you are still having trouble, here are some common mistakes to check for:
setParams
on the action before enqueuing it? Are the parameter names spelled correctly?