function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
satish waghsatish wagh 

Uncaught Error in $A.getCallback() [Cannot read property 'label' of undefined] Callback failed:

I have created the Multiselect picklist component and got above error while loading component. Any help would be appreciated!.
Please see below code:

Client Side Controller file :

({
    doInit:function(component,event,helper){
       
        helper.getOpetionsHelper(component);       
    },
    openDropdown:function(component,event,helper){
        $A.util.addClass(component.find('dropdown'),'slds-is-open');
        $A.util.removeClass(component.find('dropdown'),'slds-is-close');
    },
    closeDropDown:function(component,event,helper){
        $A.util.addClass(component.find('dropdown'),'slds-is-close');
        $A.util.removeClass(component.find('dropdown'),'slds-is-open');
    },
    selectOption:function(component,event,helper){        
        var label = event.currentTarget.id.split("#BP#")[0];
        var isCheck = event.currentTarget.id.split("#BP#")[1];
        helper.getOpetionsHelper(component,label,isCheck);
    }
})

Helper File : 

({
    getOpetionsHelper: function(component)
{
          var action = component.get("c.getOptions");
        
    action.setCallback(this, function(response) {
        var state  = response.getState();
        var optionResult = response.getReturnValue();
        console.log('response '+JSON.stringify(optionResult));
         var opt = new Array();
        for(var i=0;i<=optionResult.length;i++){  

            opt.push({isChecked:optionResult[i].isChecked,label:optionResult[i].label});
        }
        component.set("v.options",opt);
    });
    
    Server Side Controller : 
    
    public class MultiSelectPicklistCls {
    
    @AuraEnabled
    public static List<PickListOptions> getOptions()
    {
        List<PickListOptions> optList = new List<PickListOptions>();
        
    optList.add(new PickListOptions('Option 1',false));
    optList.add(new PickListOptions('Option 2',true));
    optList.add(new PickListOptions('Option 3',false));
    return optList;
    }

    // wrapper class 
    public class PickListOptions
    {
        @AuraEnabled 
        public String label {get;set;}
        @AuraEnabled 
        public boolean isChecked {get;set;}
        public PickListOptions(String label,boolean isChecked){
            this.label = label;
            this.isChecked = isChecked;
        }
    }
}
Best Answer chosen by satish wagh
vishal-negandhivishal-negandhi

Hi Satish, 

You need to change this part of your code to fix this error

 

for(var i=0;i<=optionResult.length;i++){  

SHOULD BE

for(var i=0; i < optionResult.length; i++){


Since you're setting i=0, you need to remove the <= and use = for the comparison. 

Hope this helps.

All Answers

vishal-negandhivishal-negandhi

Hi Satish, 

You need to change this part of your code to fix this error

 

for(var i=0;i<=optionResult.length;i++){  

SHOULD BE

for(var i=0; i < optionResult.length; i++){


Since you're setting i=0, you need to remove the <= and use = for the comparison. 

Hope this helps.

This was selected as the best answer
satish waghsatish wagh
Thank you very much, Vishal!