<?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 Javascript SDK Normal Search: Use return instead of console.log in Splunk Dev</title>
    <link>https://community.splunk.com/t5/Splunk-Dev/Javascript-SDK-Normal-Search-Use-return-instead-of-console-log/m-p/504386#M9031</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I have used the&amp;nbsp;&lt;SPAN&gt;Search_normal.js example that runs a normal search,&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;prints the job statistics and search results. How do I return the search results (instead of logging to console) so that I can pass the result data into another js file?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;The code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;var splunkjs = require('splunk-sdk');
var Async  = splunkjs.Async;

exports.main = function(opts, callback) {
    opts = opts || {};
    
    var username = opts.username    || "*******";
    var password = opts.password    || "********";
    var scheme   = opts.scheme      || "https";
    var host     = opts.host        || "localhost";
    var port     = opts.port        || "8089";
    var version  = opts.version     || "default";
    
    var service = new splunkjs.Service({
        username: username,
        password: password,
        scheme: scheme,
        host: host,
        port: port,
        version: version
    });

    Async.chain([
            // Login
            function(done) {
                service.login(done);
            },
            // Perform the search
            function(success, done) {
                if (!success) {
                    done("Error logging in");
                }
                
                service.search("search index=cog-censor-allow", {}, done);
            },
            // Wait until the job is done
            function(job, done) {
                job.track({}, function(job) {
                    // Ask the server for the results
                    job.results({}, done);
                });
            },
            // Print out the statistics and get the results
            function(results, job, done) {
                // Print out the statistics
                /*console.log("Job Statistics: ");
                console.log("  Event Count: " + job.properties().eventCount);
                console.log("  Disk Usage: " + job.properties().diskUsage + " bytes");
                console.log("  Priority: " + job.properties().priority);*/

                // Find the index of the fields we want
                var rawIndex = results.fields.indexOf("_raw");
                var sourcetypeIndex = results.fields.indexOf("sourcetype");
                var userIndex = results.fields.indexOf("user");
                
                // Print out each result and the key-value pairs we want
                console.log("Results: ");
                for(var i = 0; i &amp;lt; results.rows.length; i++) {
                    console.log("  Result " + i + ": ");
                    console.log("    sourcetype: " + results.rows[i][sourcetypeIndex]);
                    console.log("    user: " + results.rows[i][userIndex]);
                    console.log("    _raw: " + results.rows[i][rawIndex]);

                }
                
                job.cancel(done);
            }
        ],
        function(err) {
            callback(err);        
        }
    );
};

if (module === require.main) {
    exports.main({}, function() {});
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;My example code is&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;located in the&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;/splunk-sdk-javascript/examples/node&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;directory.&amp;nbsp;&lt;A href="https://dev.splunk.com/enterprise/docs/javascript/sdk-javascript/sdkjavascriptexamples/cmdlinedkjavascript" target="_blank" rel="noopener"&gt;https://dev.splunk.com/enterprise/docs/javascript/sdk-javascript/sdkjavascriptexamples/cmdlinedkjavascript&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 17 Jun 2020 17:23:33 GMT</pubDate>
    <dc:creator>mginsbu</dc:creator>
    <dc:date>2020-06-17T17:23:33Z</dc:date>
    <item>
      <title>Javascript SDK Normal Search: Use return instead of console.log</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Javascript-SDK-Normal-Search-Use-return-instead-of-console-log/m-p/504386#M9031</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I have used the&amp;nbsp;&lt;SPAN&gt;Search_normal.js example that runs a normal search,&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;prints the job statistics and search results. How do I return the search results (instead of logging to console) so that I can pass the result data into another js file?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;The code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;var splunkjs = require('splunk-sdk');
var Async  = splunkjs.Async;

exports.main = function(opts, callback) {
    opts = opts || {};
    
    var username = opts.username    || "*******";
    var password = opts.password    || "********";
    var scheme   = opts.scheme      || "https";
    var host     = opts.host        || "localhost";
    var port     = opts.port        || "8089";
    var version  = opts.version     || "default";
    
    var service = new splunkjs.Service({
        username: username,
        password: password,
        scheme: scheme,
        host: host,
        port: port,
        version: version
    });

    Async.chain([
            // Login
            function(done) {
                service.login(done);
            },
            // Perform the search
            function(success, done) {
                if (!success) {
                    done("Error logging in");
                }
                
                service.search("search index=cog-censor-allow", {}, done);
            },
            // Wait until the job is done
            function(job, done) {
                job.track({}, function(job) {
                    // Ask the server for the results
                    job.results({}, done);
                });
            },
            // Print out the statistics and get the results
            function(results, job, done) {
                // Print out the statistics
                /*console.log("Job Statistics: ");
                console.log("  Event Count: " + job.properties().eventCount);
                console.log("  Disk Usage: " + job.properties().diskUsage + " bytes");
                console.log("  Priority: " + job.properties().priority);*/

                // Find the index of the fields we want
                var rawIndex = results.fields.indexOf("_raw");
                var sourcetypeIndex = results.fields.indexOf("sourcetype");
                var userIndex = results.fields.indexOf("user");
                
                // Print out each result and the key-value pairs we want
                console.log("Results: ");
                for(var i = 0; i &amp;lt; results.rows.length; i++) {
                    console.log("  Result " + i + ": ");
                    console.log("    sourcetype: " + results.rows[i][sourcetypeIndex]);
                    console.log("    user: " + results.rows[i][userIndex]);
                    console.log("    _raw: " + results.rows[i][rawIndex]);

                }
                
                job.cancel(done);
            }
        ],
        function(err) {
            callback(err);        
        }
    );
};

if (module === require.main) {
    exports.main({}, function() {});
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;My example code is&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;located in the&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;/splunk-sdk-javascript/examples/node&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;directory.&amp;nbsp;&lt;A href="https://dev.splunk.com/enterprise/docs/javascript/sdk-javascript/sdkjavascriptexamples/cmdlinedkjavascript" target="_blank" rel="noopener"&gt;https://dev.splunk.com/enterprise/docs/javascript/sdk-javascript/sdkjavascriptexamples/cmdlinedkjavascript&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jun 2020 17:23:33 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Javascript-SDK-Normal-Search-Use-return-instead-of-console-log/m-p/504386#M9031</guid>
      <dc:creator>mginsbu</dc:creator>
      <dc:date>2020-06-17T17:23:33Z</dc:date>
    </item>
    <item>
      <title>Re: Javascript SDK Normal Search: Use return instead of console.log</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Javascript-SDK-Normal-Search-Use-return-instead-of-console-log/m-p/526383#M9032</link>
      <description>&lt;P&gt;HI mginsbu,&lt;/P&gt;&lt;P&gt;Did u got the right answer. I am also encounter like this problem. How did u solve ,can you please elaborate&amp;nbsp; that ?&lt;BR /&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Sun, 25 Oct 2020 20:45:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Javascript-SDK-Normal-Search-Use-return-instead-of-console-log/m-p/526383#M9032</guid>
      <dc:creator>ccmoeaung</dc:creator>
      <dc:date>2020-10-25T20:45:46Z</dc:date>
    </item>
    <item>
      <title>Re: Javascript SDK Normal Search: Use return instead of console.log</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Javascript-SDK-Normal-Search-Use-return-instead-of-console-log/m-p/535940#M9033</link>
      <description>&lt;P&gt;Hi, I am also interested, Did any one figure it out?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Jan 2021 03:25:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Javascript-SDK-Normal-Search-Use-return-instead-of-console-log/m-p/535940#M9033</guid>
      <dc:creator>Salah2019</dc:creator>
      <dc:date>2021-01-15T03:25:14Z</dc:date>
    </item>
  </channel>
</rss>

