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
VIKASH CHAND KUMARVIKASH CHAND KUMAR 

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);
    }
})
Raj VakatiRaj Vakati
Can you  share the ContactList  compomnet ?? Its must be looks like beloww
 
<aura:component controller="yournamespace.ContactController">

    <aura:attribute name="contacts" type="Contact[]"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />

    <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>

 
Naval Sharma4Naval Sharma4
Do you have any defined parameters in your APPLICATION/COMPONENT event?

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
VIKASH CHAND KUMARVIKASH CHAND KUMAR
This is the event i have.

<aura:event type="APPLICATION" description="Event template" >
    
    <aura:attribute name="searchKey" type="String" />
</aura:event>
VIKASH CHAND KUMARVIKASH CHAND KUMAR
This is the ContactList  Component i have.

<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>
Richa Upadhyay 43Richa Upadhyay 43
@Vikas,

Did you get the solution? If yes, what was the issue?

Thanks,
Richa
pentayah yuvrajpentayah yuvraj
Did someone find the solution?
i am facing the same issue.
tech geektech geek
My app not working.facing below error:
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,
 
Mayur MehtaMayur Mehta
I am new to Lightning aura component and I was also facing the similar issue, however i have made 2 changes in the code and it is now working for me.

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);
        }
    }
})
Darshan BendreDarshan Bendre
The If(token!=null) condition, wrapping remaining code was missing.
thanks @Mayur Mehta. It worked for me too.