0

What I want to do is to access the lists in the js controller from the apex. Now I have only one list in apex but in the future it's many. Now ListAllAccounts in js controller shows Undefined through(console.log).I am not asking you to provide me whole code. I want to know why it's not working? please check the code below,

public class   allAccounts {
  
        public class Response {
     
        @AuraEnabled public List<account> AllAccounts {get;set;}
        @AuraEnabled public List<String> strList {get;set;}
        
        public Response() {
            AllAccounts = new List<account>();      
        }
    }

    
    @AuraEnabled
     public static Response getAllAccounts() {
        Response responseObj = new Response();
       responseObj.AllAccounts = new List<account>([SELECT Id,Name FROM Account]);
             return responseObj;
     
    }

}

js controller

({
    doInit : function(component, event, helper) {
        var action= component.get('c.getAllAccounts')
        
        action.setCallback(this,               
             function(response){
            var responseValue = response.getReturnValue();
       
         component.set('v.allAccountsList',responseValue.AllAccounts);
                 console.log ('value'+component.set('v.allAccountsList',responseValue.AllAccounts));
                
                 
        },'SUCCESS');
        $A.enqueueAction(action,false)
        
    }
    

        
    
})

cmp

<aura:component   controller="allAccounts" >
    <aura:attribute name="allAccountsList" type="Account[]"/>
     <aura:attribute name="count" type="string"/>
    <aura:handler  name="init"  value ="{!this }" action ="{!c.doInit}"/>
   
    
    <table class="slds-table slds-table_cell-buffer slds-table_bordered" aria-labelledby="element-with-table-label other-element-with-table-label">
  <thead>
    <tr class="slds-line-height_reset">
      <th class="" scope="col">
        <div class="slds-truncate" title="Account Name">Account Name</div>
      </th>
      <th class="" scope="col">
        <div class="slds-truncate" title="Totoal Contact">Totoal Contact</div>
      </th>
      <th class="" scope="col">
        <div class="slds-truncate" title="Total Opportunity">Total Opportunity</div>
      </th>
     
    </tr>
  </thead>
  <tbody>
    
      <aura:iteration items="{v.allAccountsList}" var="acc">
        
    <tr class="slds-hint-parent"><td>
      
  </td><td>{!acc.name}</td>
      
     
          
         </tr>
       
       </aura:iteration>
  </tbody>
</table>
     
</aura:component>

1 Answer 1

0

Oh boy couple of things here but let's go to the direct problem.

Why console log returned undefined:

You're trying to show what component.set is returning and not what is assigned to the aura variable.

So something like this would work:

console.log (responseValue.AllAccounts);

instead of

console.log ('value'+component.set('v.allAccountsList',responseValue.AllAccounts));

Second thing: you've missed ! at <aura:iteration items="{v.allAccountsList}" var="acc">
so correct one is <aura:iteration items="{!v.allAccountsList}" var="acc">

Third thing:

  <aura:attribute name="allAccountsList" type="List" />

You've tried to set parametered Array here (to Account) what you are returning from backend is not Account at all from the beginning, but have

allAccounts.Response

type. If you are not solid on strict types it's easier to use List/Array (but maybe I'm biased here as Aura had historically problems with attribute types a lot)

Final thing

<td>{!acc.Name}</td>

Name should be written with capital as Javascript actually is case sensitive.

2
  • thanks, it s worked
    – user91246
    Jun 16, 2021 at 10:31
  • If you're satisfied with the answer please mark it as accepted so another people would know it is valid without reading comments :) Jun 16, 2021 at 10:41

You must log in to answer this question.