You need to sign in to do that
Don't have an account?

Cannot read property 'indexOf' of undefined
I am always getting this error "ContactDetails$controller$locationChange [Cannot read property 'indexOf' of undefined] Failing descriptor: {ContactDetails$controller$locationChange}" Not sure what is wrong, can you please help
My component is
<aura:component controller="ContactController">
<aura:attribute name="contact" type="Contact" default="{'sobjectType': 'Contact'}"/>
<aura:handler event="aura:locationChange" action="{!c.locationChange}"/>
<div class="details">
<h1>{!v.contact.Name}</h1>
<h3>{!v.contact.Account.Name}</h3>
<h3>{!v.contact.Title}</h3>
<p>{!v.contact.Phone}</p>
{!v.contact.MobilePhone}
</div>
</aura:component>
and Controller is
({
locationChange : function(component, event, helper) {
var token=event.getParam("token");
if(token.indexOf('contact/')===0)
{
var contactId=token.substr(token.indexOf('/')+1);
var action=component.get("c.findById");
action.setParams({"contactId":contactId});
}
action.setCallback(this,function(response){
component.set("v.contacttt",response.getReturnValue());
})
$A.enqueueAction(action);
}
})
My component is
<aura:component controller="ContactController">
<aura:attribute name="contact" type="Contact" default="{'sobjectType': 'Contact'}"/>
<aura:handler event="aura:locationChange" action="{!c.locationChange}"/>
<div class="details">
<h1>{!v.contact.Name}</h1>
<h3>{!v.contact.Account.Name}</h3>
<h3>{!v.contact.Title}</h3>
<p>{!v.contact.Phone}</p>
{!v.contact.MobilePhone}
</div>
</aura:component>
and Controller is
({
locationChange : function(component, event, helper) {
var token=event.getParam("token");
if(token.indexOf('contact/')===0)
{
var contactId=token.substr(token.indexOf('/')+1);
var action=component.get("c.findById");
action.setParams({"contactId":contactId});
}
action.setCallback(this,function(response){
component.set("v.contacttt",response.getReturnValue());
})
$A.enqueueAction(action);
}
})
Look at the following link where you can find an example
https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/events_application_example.htm
<aura:event type="APPLICATION" description="Event template" >
<aura:attribute name="searchKey" type="String" />
</aura:event>
<aura:component controller="ContactController">
<aura:attribute name="contacts" type="Contact[]" />
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<aura:handler event="c:SearchKeychange" action="{!c.searchKeychange}" />
<ul class="list-group">
<aura:iteration items="{!v.contacts}" var="contact">
<li class="list-group-item">
<a href="{!'#contact/'+contact.Id}">
<p> {!contact.Name}</p>
<p> {!contact.Phone}</p>
</a>
</li>
</aura:iteration>
</ul>
</aura:component>
Did you get the solution? If yes, what was the issue?
Thanks,
Richa
i am facing the same issue.
This page has an error. You might just need to refresh it. Action failed: c:conDetails$controller$locationChange [token.indexOf is not a function] Failing descriptor: {c:conDetails$controller$locationChange}
My component is:
conDetails.cmp:
<aura:component controller="conList">
<aura:attribute name="contact" type="Contact" default="{'sobjectType':'Contact'}"/>
<aura:handler event="aura:locationChange" action="{!c.locationChange}"/>
<div class="details">
<h1>{!v.contact.Name}</h1>
<h3>{!v.contact.Phone}</h3>
<h3>{!v.contact.Title}</h3>
<h3>{!v.contact.Account.Name}</h3>
</div>
</aura:component>
My Controller is:
conDetailsController:
({
locationChange : function(component, event, helper) {
var token=event.getParams("token");
if(token.indexOf('contact/')===0)
{
var ConId=token.substring(token.indexOf('/')+1);
var action=component.get("c.findConById");
action.setParams({"contactId":ConId});
action.setCallback(this,function(a){
component.set("v.contact",a.getReturnValue());
});
$A.enqueueAction(action);
}
}
})
Any inputs shall be highly appreiated.Thanks,
1. In controller i have used 'var token=event.getParam("token");' instead of 'var token=event.getParams("token");' (Use of getParam instead of getParams
2. After that line i have just add one more condition 'if(token!=null)'
Below are my code:
Component:
<aura:component controller="ContactConroller" >
<aura:attribute name="conForDetails" type="Contact" default="{'sobjectype':'Contact'}"/>
<aura:handler event="aura:locationChange" action="{!c.locationChange}"/>
<div>
<h1>{!v.conForDetails.Name}</h1>
<h1>{!v.conForDetails.Account.Name}</h1>
<h1>{!v.conForDetails.Title}</h1>
<h1>{!v.conForDetails.Phone}</h1>
</div>
</aura:component>
Controller:
({
locationChange : function(component, event, helper) {
var token=event.getParam("token");
if(token!=null)
{
if(token.indexOf('contact/')===0)
{
var conID = token.substr(token.indexOf('/')+1);
var action=component.get("c.searchContactById");
action.setParams({"contactID":conID});
}
action.setCallback(this,function(a)
{
component.set("v.conForDetails",a.getReturnValue());
})
$A.enqueueAction(action);
}
}
})
thanks @Mayur Mehta. It worked for me too.