Dashboards & Visualizations

How to get search results from SplunkJS on click of a button?

scottsavaresevi
Path Finder

I'm trying to follow the instructions here, but making the app my own a bit. I have converted my XML dashboard to HTML and am working on editing the JavaScript to make it do something. I have a button that when pressed needs to access the results of search1 (which is the manager for a TableElement) and grab the values in one of the rows. I have this so far:

$("#closeIssue").click(function() {
            // Get the value of the key ID field
            var tokens = mvc.Components.get("default");
            var form_keyid = tokens.get("form.keyid");
            var mySearch = splunkjs.mvc.Components.getInstance("search1");
            console.log( "mysearch:", mySearch );
            var myResults = mySearch.data("results", {count:0});
            console.log( "myresults:", myResults );
            var myrd = myResults.data();
            console.log( "myrd:", myrd );
            var rows = myrd.rows;
            console.log( "rows:", rows );
            return false;
}

The problem is that the "rows" variable is undefined. Looking at the Google Chrome JavaScript console, myResults has data. Looking in the object in _data are the rows I expect to be there. However, when I call myResults.data(), it returns undefined. Any thoughts on how to get the data?

Thanks in advance

0 Karma
1 Solution

scottsavaresevi
Path Finder

I've solved my own problem. The main issue is that I just did not embrace the use of Javascript callbacks. This is just wrong:

var myResults = mySearch.data("results", {count:0});
var myrd = myResults.data();

myResults didn't have the data yet. It runs in the background. The idea being that it may take a while to get the data so don't block the code from running. So I had to set up a callback on the results once the data is loaded:

var myResults = mySearch.data("results", {count:0});
myResults.on( "data", function() {
    var rows = myResults.data().rows;
    // AND SO ON
} );

I would up embedding quite a few callbacks in there, so I have to go in and de-nest all my blocks as predefined functions to make it easier to read.

View solution in original post

scottsavaresevi
Path Finder

I've solved my own problem. The main issue is that I just did not embrace the use of Javascript callbacks. This is just wrong:

var myResults = mySearch.data("results", {count:0});
var myrd = myResults.data();

myResults didn't have the data yet. It runs in the background. The idea being that it may take a while to get the data so don't block the code from running. So I had to set up a callback on the results once the data is loaded:

var myResults = mySearch.data("results", {count:0});
myResults.on( "data", function() {
    var rows = myResults.data().rows;
    // AND SO ON
} );

I would up embedding quite a few callbacks in there, so I have to go in and de-nest all my blocks as predefined functions to make it easier to read.

View solution in original post

scottsavaresevi
Path Finder

Looks like the link for the instructions I was trying to follow was not allowed in the post. The instructions I am following are the "Use KV Store with a simple app" tutorial over on dev.splunk.com. In that tutorial they have a delete button that deletes and entry in a collection using the REST API. My button is updating a row and changing one field in the row. But to do that I need to get the current value of the fields in the row out of the search so that I can use include it in the REST call.

Thanks

0 Karma
Did you miss .conf21 Virtual?

Good news! The event's keynotes and many of its breakout sessions are now available online, and still totally FREE!