I'm trying to create a simple component that will query Contacts whose Ice_Cream_Preference__c field is equal to the current Account's Ice_Cream_Specialty__c field and display those names in an iteration. On click of the lightning:button I'm getting the following error : "$A.getCallback() throwing uncaught error: [key.indexOf is not a function]". I don't know where key.indexOf is coming from. Can anybody point out the error in my code? Much appreciated.
getInterestedContacts.cmp
<aura:component implements="force:lightningQuickAction,force:hasRecordId"
controller="findIceCreamContact">
<aura:attribute name="Contacts" type="Contact[]"
default="{'sobjectType':'Contact',
'Name':'Rodrigo Duterte'}"/>
<lightning:button label="Find Matches" onclick="{!c.myAction}"/>
<h3>Contact Matches</h3>
<aura:iteration items="{!v.Contacts}" var="con">
{!con.Name}, {!con.Ice_Cream_Preference__c}
<br/>
</aura:iteration>
getInterestedContactsController.js
({
myAction : function(component, event, helper) {
var matchConts = component.get("c.getContacts");
var recordId = component.get("v.recordId");
if(recordId==null) return;
matchConts.setParams({
"accountId":recordId
});
matchConts.setCallback(this, function(response){
var state = response.getState();
if (state === "SUCCESS"){
component.set('v.Contacts', response.getReturnValue());
}
else{
console.log("failed with state: "+ state);
}
});
$A.enqueueAction(matchConts);
}
})
findIceCreamContact.apxc
public with sharing class findIceCreamContact {
@AuraEnabled
public static Contact[] getContacts(ID accountId){
List<Account> foundAcct = [SELECT Ice_Cream_Specialty__c, Id
FROM Account
WHERE ID=:accountId];
return [SELECT Id, Ice_Cream_Preference__c
FROM Contact
WHERE Ice_Cream_Preference__c =:foundAcct[0].Ice_Cream_Specialty__c];
}
}
Best Answer
You are missing the
Name
field from your Contact Query. You are referencing it in your iteration.