<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: What is the proper way for listening SearchManager results in JavaScript in Dashboards &amp; Visualizations</title>
    <link>https://community.splunk.com/t5/Dashboards-Visualizations/What-is-the-proper-way-for-listening-SearchManager-results-in/m-p/419307#M27635</link>
    <description>&lt;P&gt;I couldn't agree more. Just FYI, there is a &lt;A href="https://docs.splunk.com/Documentation/WebFramework"&gt;Web Framework Component Reference&lt;/A&gt; which at least covers the basics. It's not as good as the splunk docs though, but they set a fairly high standard.&lt;/P&gt;</description>
    <pubDate>Thu, 18 Apr 2019 10:01:35 GMT</pubDate>
    <dc:creator>jeffland</dc:creator>
    <dc:date>2019-04-18T10:01:35Z</dc:date>
    <item>
      <title>What is the proper way for listening SearchManager results in JavaScript</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/What-is-the-proper-way-for-listening-SearchManager-results-in/m-p/419304#M27632</link>
      <description>&lt;P&gt;I tried the following ways:&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;Way 1&lt;/STRONG&gt;: One method is to run search and wait for its results is as follows:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;require([
    "splunkjs/mvc/searchmanager",
    "splunkjs/mvc/simplexml/ready!"
], function (
    SearchManager
) {
    // search to run
    var defaultTableSearch = new SearchManager({
        id: "populate-default-table",
        preview: true,
        cache: true,
        search: mvc.tokenSafe(`##your search query here##`)
    });
    // fetch results from search
    var defaultResults = defaultTableSearch.data("results");

    // wait for the search to return results
    defaultResults.on("data", function () {
        var data = defaultResults.data();
        // do something with data...
    });
});
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG&gt;Problem with Way 1&lt;/STRONG&gt;: With the above method, if our search doesn't result in any data then it will not trigger &lt;CODE&gt;on("data" ...);&lt;/CODE&gt; at all.&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;Way 2&lt;/STRONG&gt;: One other solution may be to listen onto &lt;CODE&gt;search:done&lt;/CODE&gt; event on search manager&lt;BR /&gt;
&lt;STRONG&gt;Problem with Way 2&lt;/STRONG&gt;: it's not guaranteed we will get results immediately after that event gets triggered.&lt;/P&gt;

&lt;P&gt;Is there something wrong I'm doing or something I'm missing? &lt;/P&gt;

&lt;P&gt;Note: Blocking search solves this but I want to find a proper way to do it asynchronously.&lt;/P&gt;

&lt;P&gt;Thanks.&lt;/P&gt;

&lt;P&gt;==================================================================================================&lt;BR /&gt;
&lt;STRONG&gt;Edit&lt;/STRONG&gt;:&lt;BR /&gt;
I was missing two arguments for &lt;CODE&gt;search:done&lt;/CODE&gt; callback function from which I can fetch the results as follows:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;defaultTableSearch.on("search:done", function (state, job) {
        if (state.content.resultCount === 0) {
            console.log("no results");
        } else {
            // Get the job from the server to display more info
            job.fetch(function (err) {
                // Get the results and display them
                job.results({}, function (err, results) {
                    // do something with results...
                    console.log(results);
                });
            });
        }
    });
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Apr 2019 05:33:22 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/What-is-the-proper-way-for-listening-SearchManager-results-in/m-p/419304#M27632</guid>
      <dc:creator>harshpatel</dc:creator>
      <dc:date>2019-04-18T05:33:22Z</dc:date>
    </item>
    <item>
      <title>Re: What is the proper way for listening SearchManager results in JavaScript</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/What-is-the-proper-way-for-listening-SearchManager-results-in/m-p/419305#M27633</link>
      <description>&lt;P&gt;The proper way to listen for result events is using &lt;CODE&gt;defaultTableSearch.data("results").on("data", callback)&lt;/CODE&gt;. If you need to also act if there are no results returned, a case in which the &lt;CODE&gt;data&lt;/CODE&gt; callback is not called, you can check your search results in the &lt;CODE&gt;search:done&lt;/CODE&gt; callback for the number of results and only run code if it is zero:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;search.on("search:done", function(state, job) {
    if (state.content.resultCount === 0) {
        alert("no results");
    }
});
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Refer to &lt;A href="https://answers.splunk.com/answers/117403/is-a-javascript-event-called-when-a-searchmanager-search-returns-no-data.html"&gt;this answer&lt;/A&gt; for a past discussion.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Apr 2019 06:18:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/What-is-the-proper-way-for-listening-SearchManager-results-in/m-p/419305#M27633</guid>
      <dc:creator>jeffland</dc:creator>
      <dc:date>2019-04-18T06:18:35Z</dc:date>
    </item>
    <item>
      <title>Re: What is the proper way for listening SearchManager results in JavaScript</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/What-is-the-proper-way-for-listening-SearchManager-results-in/m-p/419306#M27634</link>
      <description>&lt;P&gt;Thanks! I wish Splunk doc for its JS stack was well documented with examples like it is for other things. &lt;/P&gt;</description>
      <pubDate>Thu, 18 Apr 2019 09:33:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/What-is-the-proper-way-for-listening-SearchManager-results-in/m-p/419306#M27634</guid>
      <dc:creator>harshpatel</dc:creator>
      <dc:date>2019-04-18T09:33:01Z</dc:date>
    </item>
    <item>
      <title>Re: What is the proper way for listening SearchManager results in JavaScript</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/What-is-the-proper-way-for-listening-SearchManager-results-in/m-p/419307#M27635</link>
      <description>&lt;P&gt;I couldn't agree more. Just FYI, there is a &lt;A href="https://docs.splunk.com/Documentation/WebFramework"&gt;Web Framework Component Reference&lt;/A&gt; which at least covers the basics. It's not as good as the splunk docs though, but they set a fairly high standard.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Apr 2019 10:01:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/What-is-the-proper-way-for-listening-SearchManager-results-in/m-p/419307#M27635</guid>
      <dc:creator>jeffland</dc:creator>
      <dc:date>2019-04-18T10:01:35Z</dc:date>
    </item>
  </channel>
</rss>

