Dashboards & Visualizations

Why am I getting “Uncaught TypeError: Cannot read property 'startSearch' of undefined” when I use mvc.components.get()?

tommywang
Explorer

xml code:

< search id= 'pie_search'>

        < query>index=mtest1 | stats sum(num) by fruit</query>

< /search>

js code:

var mySearch = mvc.Components.get('pie_search');
mySearch.startSearch();

Error:

Uncaught TypeError: Cannot read property 'startSearch' of undefined

Can anyone please tell me what I am missing?

0 Karma
1 Solution

jeffland
SplunkTrust
SplunkTrust

Not sure you get your search correctly, where is that code from? Try

var mySearch = splunkjs.mvc.Components.getInstance("pie_search")

View solution in original post

jeffland
SplunkTrust
SplunkTrust

Not sure you get your search correctly, where is that code from? Try

var mySearch = splunkjs.mvc.Components.getInstance("pie_search")

tommywang
Explorer

It doesn't work for me. The error is the same.
However, I can use both "var mySearch = splunkjs.mvc.Components.getInstance("search1") " and "var mySearch = mvc.Components.get('search1');" to get the object of searchManager in my js file.
The problem is that I can't get the object according to the "id" of search label, like "var mySearch = mvc.Components.get('pie_search')". No matter how I change the "id" of search label, the only way to acquire the object is by the "id" search1.

0 Karma

jeffland
SplunkTrust
SplunkTrust

When you open the dashboard in your browser and check the html code, can you see your searches in the script part after the layout part? They should be after the part about tokens, and there you can confirm what id they have. It looks like somehow, your id isn't set correctly, but I assume you have other searches (without unique ids) which you then select with "search1" and "search2".

0 Karma

tommywang
Explorer

Thanks, jeffland.
I find out that I can add id attribute of my search from 6.2. And that really makes sense.
Actually, before 6.2 (like 6.1), there is no id attribute of search, and splunk will give the id to search by default. The default id will be search1, search2...according to the sequence of searches.

0 Karma

tommywang
Explorer

PS. The version of my Splunk is 6.1. Doesn't is matter?

0 Karma

jeffland
SplunkTrust
SplunkTrust

Oh! Confer http://docs.splunk.com/Documentation/Splunk/6.1/Viz/PanelreferenceforSimplifiedXML to see that in 6.1, there is no such thing as an id attribute of your search. That is introduced later. Only your panels have ids, I don't know if you can access your search from the id of your panels.

Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas     Cisco Live 2026 is almost here, and this ...

What Is the Name of the USB Key Inserted by Bob Smith? (BOTS Hint, Not the Answer)

Hello Splunkers,   So you searched, “what is the name of the usb key inserted by bob smith?”  Not gonna lie… ...

Automating Threat Operations and Threat Hunting with Recorded Future

    Automating Threat Operations and Threat Hunting with Recorded Future June 29, 2026 | Register   Is your ...