Dashboards & Visualizations
Highlighted

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

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
Highlighted

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

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

Highlighted

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

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
Highlighted

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

Explorer

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

0 Karma
Highlighted

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

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.

Highlighted

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

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
Highlighted

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

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