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
Champion

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
Champion

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
Champion

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
Champion

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.

Get Updates on the Splunk Community!

Splunk Cloud | Empowering Splunk Administrators with Admin Config Service (ACS)

Greetings, Splunk Cloud Admins and Splunk enthusiasts! The Admin Configuration Service (ACS) team is excited ...

Tech Talk | One Log to Rule Them All

One log to rule them all: how you can centralize your troubleshooting with Splunk logs We know how important ...

Splunk Security Content for Threat Detection & Response, Q1 Roundup

Join Principal Threat Researcher, Michael Haag, as he walks through: An introduction to the Splunk Threat ...