+ Start a Discussion
bharath kumar 52bharath kumar 52 

Not able to get data from multiple rows in lightning component

Hi All,

I am trying to build a search component which takes multiple criteria so i am adding fields, criteria and the search string. When i try to add more than 1 criteria in my lightning component i am getting this error
"This page has an error. You might just need to refresh it. Action failed: c:FilterComponent$controller$searchProducts [Cannot read property 'get' of undefined] Failing descriptor: {c:FilterComponent$controller$searchProducts}"
Please help me fix this. I also tried checking if its an array or not but that doesn't work in my favour.
User-added image


 
Component :

<aura:component controller="SearchFilterController" Implements="force:appHostable,flexipage:availableForRecordHome,force:hasRecordId">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:attribute name="prodWrapper" type="list"/>
    <aura:attribute type="Object" name="testAttribute" />
    <aura:attribute name="accountList" type="Account[]"/>
    <aura:attribute name="productFields" type="list"/>
    <aura:attribute name="criteriaType" type="list"/>
    <lightning:card>
        <div class="slds-m-around--xx-large">
            <div class="slds-float_right slds-p-bottom_small">
                <h1 class="slds-page-header__title">Add Row 
                    <lightning:buttonIcon iconName="utility:add"  size="large" variant="bare" alternativeText="Add" onclick="{!c.addRow}"/>
                </h1>
            </div>
            <div class="container-fluid">        
                <table class="slds-table slds-table_bordered slds-table_cell-buffer"> 
                    <thead>
                        <tr class="slds-text-title_caps">
                            <th scope="col">
                                <div class="slds-truncate">Sr. No</div>
                            </th>
                            <th scope="col">
                                <div class="slds-truncate" title="Field Name">Field Name</div>
                            </th>
                            <th scope="col">
                                <div class="slds-truncate" title="Criteria">Criteria</div>
                            </th>
                            <th scope="col">
                                <div class="slds-truncate" title="Value">Value</div>
                            </th>  

                            <th scope="col">
                                <div class="slds-truncate" title="Action">Action</div>
                            </th>
                        </tr>
                    </thead>   
                    <tbody>      
                        <aura:iteration items="{!v.prodWrapper}" var="acc" indexVar="index">
                            <!--{!v.prodWrapper.size} -->
                            <tr>
                                <td> 
                                    {!'index'+index}
                                </td>
                                <td>
                                    <!--<lightning:input name="fieldName" type="text" maxlength="50" value="{!v.testAttribute.fieldNames}" />
                                {!index}
									-->
                                     <lightning:select aura:id="PicklistId" label="Select a field" name="fieldName" onchange="{c.changeHandler}" >
                                                <option value="" text="- None -" /> 
                                                <aura:iteration items="{!v.productFields}" var="field">
                                                    <option value="{!field}" text="{!field}" />  
                                                </aura:iteration>
                                            </lightning:select>
								</td>
                                <td>
                                   <!-- <lightning:input name="criteria" type="string" maxlength="30" value="{!acc.criteriaType}" />
                                -->
                                    <lightning:select aura:id="PicklistId2" label="Select a criteria" name="criteria" >
                                                <option value="" text="- None -" /> 
                                                <aura:iteration items="{!v.criteriaType}" var="per">
                                                    <option value="{!per}" text="{!per}" />  
                                                </aura:iteration>
                                            </lightning:select>
								</td>
                                <td>
                                    <lightning:input name="searchFieldValue" type="text" value="{!acc.searchStr}" />
                                </td>
   
                                <td>
                                    <a onclick="{!c.removeRecord}" data-record="{!index}">
                                        <lightning:icon iconName="utility:delete" size="small" alternativeText="Delete"/>
                                        <span class="slds-assistive-text">Delete</span>
                                    </a>
                                </td> 
                            </tr>
                        </aura:iteration>
                    </tbody>
                </table>
                <div class="slds-align_absolute-center slds-p-top_small">
                    <lightning:button variant="brand" label="Submit" title="Brand action" onclick="{!c.searchProducts}" />
                </div>
            </div>
        </div>
    </lightning:card>
</aura:component>

==================

JS Controller :

({	
    doInit:function(component, event, helper) {
        
        var action = component.get("c.displayFieldsAndCriteria");
            
        action.setCallback(this, function(response) {
                //get response status 
                var state = response.getState();
                if (state === "SUCCESS") {
                    //set empty account list
                   var respValue=JSON.parse(response.getReturnValue());
                    component.set("v.testAttribute",respValue);
                    //var testValue = component.set("v.testAttribute",response.getReturnValue());
                    var respVal =response.getReturnValue();
                    //console.log('respVal >>> '+respVal);
                    console.log('v.testAttribute value >>>>'+respValue);
                    console.log('criteriaType value >>>>'+respValue.criteriaType);
                    console.log('searchStr value >>>>'+respValue.searchStr);
                   // console.log('fieldNames value >>>>'+JSON.stringify(respValue.fieldNames));
                    //console.log('respValue.fieldNames typeof >>>> '+typeof(respValue.fieldNames));
                    var result=respValue.fieldNames;
                    console.log('result stringified >>>>>> '+result.typeof);
                    component.set("v.productFields",respValue.fieldNames);
                    component.set("v.criteriaType",respValue.criteriaType);
                    /*var criteriaTypeVar= component.get("v.criteriaType");
                    console.log('criteriaTypeVar >>>> '+criteriaTypeVar[0]);
                    var productFieldsVar= component.get("v.productFields");
                    console.log('productFields >>>> '+productFieldsVar);*/
                    alert('data received from class');
                }
            }); 
            $A.enqueueAction(action);

    },
  	
    addRow: function(component, event, helper) {
        //get the List from component  
        var pwList = component.get("v.prodWrapper");
        
        //Add New criteria
        pwList.push({
            'fieldNames': '',
            'criteriaType': '',
            'searchStr': ''
        });
        component.set("v.prodWrapper", pwList);
    },
    
    removeRecord: function(component, event, helper) {

        var criteriaList = component.get("v.prodWrapper");

        var selectedItem = event.currentTarget;
        //Get the selected item index
        var index = selectedItem.dataset.record;
        criteriaList.splice(index, 1);
        component.set("v.prodWrapper", criteriaList);
    },
    
    changeHandler:function(component, event, helper){
    var count=event.target.id;
    console.log('input id >>>> '+count);
	},
    	
   searchProducts: function(component, event, helper) {  
       var a= component.get("v.prodWrapper");
       console.log(a);
        var fName=component.find("PicklistId").get("v.value");
        for(var i=0;i<a.length;i++){
            
                console.log('fName >>>>'+ fName.length);
                a[i].fieldNames=fName;
                console.log('a value >>>> '+JSON.stringify(a));
           
        }
         /*var b= component.find("PicklistId2").get("v.value");
            var c= component.find("PicklistId").get("v.value");*/
        // use compo.find.get for picklist fields and while setting parameters create a json string
        // send the created json string to server
          /*console.log('typeof c>> '+typeof(c) +' '+JSON.stringify(c));
        console.log('typeof b >> '+typeof(b) +' '+JSON.stringify(b));
       console.log('typeof a >> '+typeof(a) +' '+JSON.stringify(a));*/
        var action = component.get("c.generateQuery");
            action.setParams({
                "prodWrapList": JSON.stringify(component.get("v.prodWrapper"))
            });
        action.setCallback(this, function(response) {
                //get response status 
                var state = response.getState();
                if (state === "SUCCESS") {
                    //set empty account list
                    component.set("v.prodWrapper", []);
                    alert('data sent to generateQuery');
                }
            }); 
            $A.enqueueAction(action);
       
    }
})





 
Best Answer chosen by bharath kumar 52
ravi soniravi soni
hi,
try below code now it's working fine.I made some changes in your cmp and js file.
<aura:component controller="SearchFilterController" Implements="force:appHostable,flexipage:availableForRecordHome,force:hasRecordId">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:attribute name="prodWrapper" type="list"/>
    <aura:attribute type="Object" name="testAttribute" />
    <aura:attribute name="accountList" type="Account[]"/>
    <aura:attribute name="productFields" type="list"/>
    <aura:attribute name="criteriaType" type="list"/>
   
    <lightning:card>
        <div class="slds-m-around--xx-large">
            <div class="slds-float_right slds-p-bottom_small">
                <h1 class="slds-page-header__title">Add Row 
                    <lightning:buttonIcon iconName="utility:add"  size="large" variant="bare" alternativeText="Add" onclick="{!c.addRow}"/>
                </h1>
            </div>
            <div class="container-fluid">        
                <table class="slds-table slds-table_bordered slds-table_cell-buffer"> 
                    <thead>
                        <tr class="slds-text-title_caps">
                            <th scope="col">
                                <div class="slds-truncate">Sr. No</div>
                            </th>
                            <th scope="col">
                                <div class="slds-truncate" title="Field Name">Field Name</div>
                            </th>
                            <th scope="col">
                                <div class="slds-truncate" title="Criteria">Criteria</div>
                            </th>
                            <th scope="col">
                                <div class="slds-truncate" title="Value">Value</div>
                            </th>  

                            <th scope="col">
                                <div class="slds-truncate" title="Action">Action</div>
                            </th>
                        </tr>
                    </thead>   
                    <tbody>      
                        <aura:iteration items="{!v.prodWrapper}" var="acc" indexVar="index">
                            <!--{!v.prodWrapper.size} -->
                            <tr>
                                <td> 
                                    {!'index'+index}
                                </td>
                                <td>
                                    <!--<lightning:input name="fieldName" type="text" maxlength="50" value="{!v.testAttribute.fieldNames}" />
                                {!index}
									-->
                                     <lightning:select aura:id="PicklistId" label="Select a field" name="fieldName" 
                                                       onchange="{!c.changeHandler}" value="{!acc.fieldNames}">
                                                <option value="" text="- None -" /> 
                                                <aura:iteration items="{!v.productFields}" var="field">
                                                    <option value="{!field}" text="{!field}" />  
                                                </aura:iteration>
                                            </lightning:select>
								</td>
                                <td>
                                   <!-- <lightning:input name="criteria" type="string" maxlength="30" value="{!acc.criteriaType}" />
                                -->
                                    <lightning:select aura:id="PicklistId2" label="Select a criteria" name="criteria" 
                                                      value="{!acc.criteriaType}">
                                                <option value="" text="- None -" /> 
                                                <aura:iteration items="{!v.criteriaType}" var="per">
                                                    <option value="{!per}" text="{!per}" />  
                                                </aura:iteration>
                                            </lightning:select>
								</td>
                                <td>
                                    <lightning:input name="searchFieldValue" type="text" value="{!acc.searchStr}" />
                                </td>
   
                                <td>
                                    <a onclick="{!c.removeRecord}" data-record="{!index}">
                                        <lightning:icon iconName="utility:delete" size="small" alternativeText="Delete"/>
                                        <span class="slds-assistive-text">Delete</span>
                                    </a>
                                </td> 
                            </tr>
                        </aura:iteration>
                    </tbody>
                </table>
                <div class="slds-align_absolute-center slds-p-top_small">
                    <lightning:button variant="brand" label="Submit" title="Brand action" onclick="{!c.searchProducts}" />
                </div>
            </div>
        </div>
    </lightning:card>
</aura:component>
 
({	
    doInit:function(component, event, helper) {
        
        var action = component.get("c.displayFieldsAndCriteria");
            
        action.setCallback(this, function(response) {
                var state = response.getState();
                if (state === "SUCCESS") {
                   var respValue=JSON.parse(response.getReturnValue());
                    component.set("v.testAttribute",respValue);
                    var respVal =response.getReturnValue();
                    console.log('v.testAttribute value >>>>'+respValue);
                    console.log('criteriaType value >>>>'+respValue.criteriaType);
                    console.log('searchStr value >>>>'+respValue.searchStr);
                    var result=respValue.fieldNames;
                    console.log('result stringified >>>>>> '+result.typeof);
                    component.set("v.productFields",respValue.fieldNames);
                    component.set("v.criteriaType",respValue.criteriaType);
                    alert('data received from class');
                }
            }); 
            $A.enqueueAction(action);

    },
  	
    addRow: function(component, event, helper) {
        //get the List from component  
        var pwList = component.get("v.prodWrapper");
        
        //Add New criteria
        pwList.push({
            'fieldNames': '',
            'criteriaType': '',
            'searchStr': ''
        });
        component.set("v.prodWrapper", pwList);
    },
    
    removeRecord: function(component, event, helper) {

        var criteriaList = component.get("v.prodWrapper");

        var selectedItem = event.currentTarget;
        //Get the selected item index
        var index = selectedItem.dataset.record;
        criteriaList.splice(index, 1);
        component.set("v.prodWrapper", criteriaList);
    },
    
    changeHandler:function(component, event, helper){
    /*    var fieldVal = event.target.value;
    console.log('input fieldVal >>>> '+fieldVal);    
        */
	},
  
   searchProducts: function(component, event, helper) {  
       var a= component.get("v.prodWrapper");
       console.log('@Developer=====> ' + JSON.stringify(a));
        //var fName=component.find("PicklistId").get("v.value");
       // var fName=component.get("v.productFieldPickValue");
        /*for(var i=0;i<a.length;i++){
            
                //console.log('fName >>>>'+ fName);
                a[i].fieldNames=fName;
                console.log('a value >>>> '+JSON.stringify(a));
           
        }*/
        var action = component.get("c.generateQuery");
            action.setParams({
                "prodWrapList": JSON.stringify(component.get("v.prodWrapper"))
            });
        action.setCallback(this, function(response) {
                //get response status 
                var state = response.getState();
            alert(state);
                if (state === "SUCCESS") {
                    //set empty account list
                    component.set("v.prodWrapper", []);
                    alert('data sent to generateQuery');
                }
             else if (state === "INCOMPLETE") {
                // do something
            }
            else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + 
                                 errors[0].message);
                    }
                } else {
                    console.log("Unknown error");
                }
            }
            }); 
            $A.enqueueAction(action);
       
    }
})

I hope above answer will help you. friend don't forget to mark it as best answer because I invested my much time over it.
Thank you

All Answers

ravi soniravi soni
hy bharath,
I need ApexCtrl and if you have helper then plz share with us. Plz share whole code. we will try to fix it.
thank you
bharath kumar 52bharath kumar 52

Hi Veer,

Below is the Apex controller. I haven't added any code to the helper js for now so the code must be good to go. Thanks again
 

Apex Controller :

public class SearchFilterController {

    @AuraEnabled
    public static string displayFieldsAndCriteria() 
	{        
        productWrapper pw = new productWrapper();
        system.debug('pw >>>>> '+pw);
        
        return json.serialize(pw);
    }
    
    @AuraEnabled
    public static void generateQuery(String prodWrapList) 
	{        
		//system.debug('prodWrapList >>>> '+prodWrapList.tostring());
        system.debug('prodWrapList serialized >>>> '+json.serialize(prodWrapList));
        system.debug('Output >>>>> '+json.deserialize(prodWrapList, List<JSON2Apex>.class));
        List<JSON2Apex> jsList=(List<JSON2Apex>)json.deserialize(prodWrapList, List<JSON2Apex>.class);
        system.debug(' jsList >>> ' +jsList);
        
        for(JSON2Apex ja :jsList){
            system.debug('Field Name >>> '+ja.fieldNames +' criteriaType >>>> '+ja.criteriaType +' searchStr >>>>'+ja.searchStr);
            
        }
        /*list<String> queryParams=(list<String>)prodWrapList;
        List<productWrapper> lstWrapper = new List<productWrapper>();*/
        //system.debug('Output >>>>> '+(List<productWrapper>)System.JSON.deserializeStrict(prodWrapList, list<productWrapper>.Class));
        
        //lstWrapper.addAll((List<productWrapper>)prodWrapList);
        //JSON.deserializeUntyped((list<string>)prodWrapList); ---->FATAL_ERROR System.TypeException: Invalid conversion from runtime type List<ANY> to List<String>
        //JSON.deserialize(prodWrapList, lstWrapper); ----> doesn't work
        //system.debug('deserialized lst >>>> '+lstWrapper);
    }
    
    
    /*public static List<string> getFields(String selectedObject){
        List<String> reqFields = new List<String>();
        Map <String,Schema.SObjectType> gd = Schema.getGlobalDescribe();
        Schema.SObjectType sobjType = gd.get(selectedObject);
        Schema.DescribeSObjectResult r = sobjType.getDescribe();
        Map<String, Schema.SObjectField> MapofField = r.fields.getMap();
        
        for(String fieldName : MapofField.keySet()) {
            Schema.SObjectField field = MapofField.get(fieldName);
            Schema.DescribeFieldResult F = field.getDescribe();
            System.debug('field-->'+field);
            reqFields.add(field);
            //System.debug('F-->'+F.getType of field 
        }
        System.debug(reqFields);
        return reqFields;
    }*/
    
    public class JSON2Apex {//used to parse json data coming in AND TO BUILD THE QUERY

	public String fieldNames;
	public String criteriaType;
	public String searchStr;

	
	public List<JSON2Apex> parse(String json) {
		return (List<JSON2Apex>) System.JSON.deserialize(json, List<JSON2Apex>.class);
	}
}
    
        public class productWrapper{// this is used to bind and display elements on the screen
            @AuraEnabled
            public String searchStr{get; set;}
            @AuraEnabled
            //public Map<String,String> fieldNames{get; set;}
            public list<String> fieldNames{get; set;}
            @AuraEnabled
            public list<String> criteriaType{get; set;}
            
            public productWrapper(){
                searchStr='';
                fieldNames= new list<String>();
                criteriaType= new list<String>();
                criteriaType.add('equal to');
                criteriaType.add('greater than or equal to');
                criteriaType.add('lesser than or equal to');
                criteriaType.add('lesser than');
                criteriaType.add('greater than');
                criteriaType.add('contains');
                
                Map<String, Schema.SObjectField> schemaFieldMap = Schema.SObjectType.Product2.fields.getMap();
                //Map<String, Object> queriedFieldValues = new Map<String, Object>();
                for (String fieldName: schemaFieldMap.keySet()) {
                    try { 
                        //system.debug('fieldName >>>'+fieldName +' mystery >>>>'+schemaFieldMap.get(fieldName));
                        fieldNames.add(fieldName); 
                    } 
                    catch(Exception e){
                        
                    }
            }
        }
    }
}
 



 

ravi soniravi soni
hi,
try below code now it's working fine.I made some changes in your cmp and js file.
<aura:component controller="SearchFilterController" Implements="force:appHostable,flexipage:availableForRecordHome,force:hasRecordId">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:attribute name="prodWrapper" type="list"/>
    <aura:attribute type="Object" name="testAttribute" />
    <aura:attribute name="accountList" type="Account[]"/>
    <aura:attribute name="productFields" type="list"/>
    <aura:attribute name="criteriaType" type="list"/>
   
    <lightning:card>
        <div class="slds-m-around--xx-large">
            <div class="slds-float_right slds-p-bottom_small">
                <h1 class="slds-page-header__title">Add Row 
                    <lightning:buttonIcon iconName="utility:add"  size="large" variant="bare" alternativeText="Add" onclick="{!c.addRow}"/>
                </h1>
            </div>
            <div class="container-fluid">        
                <table class="slds-table slds-table_bordered slds-table_cell-buffer"> 
                    <thead>
                        <tr class="slds-text-title_caps">
                            <th scope="col">
                                <div class="slds-truncate">Sr. No</div>
                            </th>
                            <th scope="col">
                                <div class="slds-truncate" title="Field Name">Field Name</div>
                            </th>
                            <th scope="col">
                                <div class="slds-truncate" title="Criteria">Criteria</div>
                            </th>
                            <th scope="col">
                                <div class="slds-truncate" title="Value">Value</div>
                            </th>  

                            <th scope="col">
                                <div class="slds-truncate" title="Action">Action</div>
                            </th>
                        </tr>
                    </thead>   
                    <tbody>      
                        <aura:iteration items="{!v.prodWrapper}" var="acc" indexVar="index">
                            <!--{!v.prodWrapper.size} -->
                            <tr>
                                <td> 
                                    {!'index'+index}
                                </td>
                                <td>
                                    <!--<lightning:input name="fieldName" type="text" maxlength="50" value="{!v.testAttribute.fieldNames}" />
                                {!index}
									-->
                                     <lightning:select aura:id="PicklistId" label="Select a field" name="fieldName" 
                                                       onchange="{!c.changeHandler}" value="{!acc.fieldNames}">
                                                <option value="" text="- None -" /> 
                                                <aura:iteration items="{!v.productFields}" var="field">
                                                    <option value="{!field}" text="{!field}" />  
                                                </aura:iteration>
                                            </lightning:select>
								</td>
                                <td>
                                   <!-- <lightning:input name="criteria" type="string" maxlength="30" value="{!acc.criteriaType}" />
                                -->
                                    <lightning:select aura:id="PicklistId2" label="Select a criteria" name="criteria" 
                                                      value="{!acc.criteriaType}">
                                                <option value="" text="- None -" /> 
                                                <aura:iteration items="{!v.criteriaType}" var="per">
                                                    <option value="{!per}" text="{!per}" />  
                                                </aura:iteration>
                                            </lightning:select>
								</td>
                                <td>
                                    <lightning:input name="searchFieldValue" type="text" value="{!acc.searchStr}" />
                                </td>
   
                                <td>
                                    <a onclick="{!c.removeRecord}" data-record="{!index}">
                                        <lightning:icon iconName="utility:delete" size="small" alternativeText="Delete"/>
                                        <span class="slds-assistive-text">Delete</span>
                                    </a>
                                </td> 
                            </tr>
                        </aura:iteration>
                    </tbody>
                </table>
                <div class="slds-align_absolute-center slds-p-top_small">
                    <lightning:button variant="brand" label="Submit" title="Brand action" onclick="{!c.searchProducts}" />
                </div>
            </div>
        </div>
    </lightning:card>
</aura:component>
 
({	
    doInit:function(component, event, helper) {
        
        var action = component.get("c.displayFieldsAndCriteria");
            
        action.setCallback(this, function(response) {
                var state = response.getState();
                if (state === "SUCCESS") {
                   var respValue=JSON.parse(response.getReturnValue());
                    component.set("v.testAttribute",respValue);
                    var respVal =response.getReturnValue();
                    console.log('v.testAttribute value >>>>'+respValue);
                    console.log('criteriaType value >>>>'+respValue.criteriaType);
                    console.log('searchStr value >>>>'+respValue.searchStr);
                    var result=respValue.fieldNames;
                    console.log('result stringified >>>>>> '+result.typeof);
                    component.set("v.productFields",respValue.fieldNames);
                    component.set("v.criteriaType",respValue.criteriaType);
                    alert('data received from class');
                }
            }); 
            $A.enqueueAction(action);

    },
  	
    addRow: function(component, event, helper) {
        //get the List from component  
        var pwList = component.get("v.prodWrapper");
        
        //Add New criteria
        pwList.push({
            'fieldNames': '',
            'criteriaType': '',
            'searchStr': ''
        });
        component.set("v.prodWrapper", pwList);
    },
    
    removeRecord: function(component, event, helper) {

        var criteriaList = component.get("v.prodWrapper");

        var selectedItem = event.currentTarget;
        //Get the selected item index
        var index = selectedItem.dataset.record;
        criteriaList.splice(index, 1);
        component.set("v.prodWrapper", criteriaList);
    },
    
    changeHandler:function(component, event, helper){
    /*    var fieldVal = event.target.value;
    console.log('input fieldVal >>>> '+fieldVal);    
        */
	},
  
   searchProducts: function(component, event, helper) {  
       var a= component.get("v.prodWrapper");
       console.log('@Developer=====> ' + JSON.stringify(a));
        //var fName=component.find("PicklistId").get("v.value");
       // var fName=component.get("v.productFieldPickValue");
        /*for(var i=0;i<a.length;i++){
            
                //console.log('fName >>>>'+ fName);
                a[i].fieldNames=fName;
                console.log('a value >>>> '+JSON.stringify(a));
           
        }*/
        var action = component.get("c.generateQuery");
            action.setParams({
                "prodWrapList": JSON.stringify(component.get("v.prodWrapper"))
            });
        action.setCallback(this, function(response) {
                //get response status 
                var state = response.getState();
            alert(state);
                if (state === "SUCCESS") {
                    //set empty account list
                    component.set("v.prodWrapper", []);
                    alert('data sent to generateQuery');
                }
             else if (state === "INCOMPLETE") {
                // do something
            }
            else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + 
                                 errors[0].message);
                    }
                } else {
                    console.log("Unknown error");
                }
            }
            }); 
            $A.enqueueAction(action);
       
    }
})

I hope above answer will help you. friend don't forget to mark it as best answer because I invested my much time over it.
Thank you
This was selected as the best answer
bharath kumar 52bharath kumar 52

Hi Veer,

Firstly, thanks a ton for fixing it. While you were busy checking my code i also took a few less travelled roads to fix this. Just as an FYI i used the dynamic aura id generated with an index variable and tried to get it but it dint work for me. So, i took a last chance(although it's not a best practice) and tried the below and it also works. But i definitely admit that yours is the best answer my friend. Meanwhile, please have a look at my code(hack : D ) below. You can also checkout the limitation on why dynamic aura id doesn't work . Here's the link https://trailblazer.salesforce.com/ideaView?id=0873A000000E8fBQAS

change in component
<!--Changed this to HTML DOM to get the id as i was not able to get the aura id-->
<select id="{!'criteria'+index}" label="Select a criteria" name="criteria" >
                                                <option value="" text="- None -" /> 
                                                <aura:iteration items="{!v.criteriaType}" var="per">
                                                    <option value="{!per}" text="{!per}" />  
                                                </aura:iteration>
                                            </select>

change in js controller
//Typical HTML-JS style of getting values if everything else fails
searchProducts2: function(component, event, helper) {  
       console.log('inside searchProducts2');
       var a= component.get("v.prodWrapper");
        console.log('inside searchProducts2 after a >>>>> '+JSON.stringify(a));
       var fieldIndexVar;
       var criteriaIndexVar;
       
        for(var i=0;i<a.length;i++){
            fieldIndexVar='index'+i;
            criteriaIndexVar='criteria'+i;
            //var fName=component.find("PicklistId").get("v.value");
            console.log('fieldIndexVar >>> '+fieldIndexVar);
            var fName=document.getElementById(fieldIndexVar).value;
                console.log('fName >>>>'+ fName);
           
        }
        
       
    }