Dashboards & Visualizations

Javascript stopped working in 9.1.1

timgren
Path Finder

Custom token script stopped working. Can anyone spot any obvious errors? It worked perfectly from version 6.x - 8.x 

I the error "A custom JavaScript error caused an issue loading your dashboard. See the developer console for more details." The console isnt very helpful. 

common.js:1702 Error: Script error for: util/console
http://requirejs.org/docs/errors.html#scripterror
at makeError (eval at e.exports (common.js:1:1), <anonymous>:166:17)
at HTMLScriptElement.onScriptError (eval at e.exports (common.js:1:1), <anonymous>:1689:36)


// Tokenize.js 

require(['jquery', 'underscore', 'splunkjs/mvc', 'util/console'], function($, _, mvc, console) {
     function setToken(name, value) {
         var defaultTokenModel = mvc.Components.get('default');
         if (defaultTokenModel) {
             defaultTokenModel.set(name, value);
         }
         var submittedTokenModel = mvc.Components.get('submitted');
         if (submittedTokenModel) {
             submittedTokenModel.set(name, value);
         }
     }

// Main

     $('.dashboard-body').on('click', '[data-on-class],[data-off-class],[data-set-token],[data-unset-token],[data-token-json]', function(e) {
         e.preventDefault();

console.log("Inside the click");
         var target = $(e.currentTarget);
console.log("here");
console.log("target.data('on-class')=" + target.data('on-class'));
 
var cssOnClass= target.data('on-class');
var cssOffClass = target.data('off-class');

if (cssOnClass) {
$("." + cssOnClass).attr('class', cssOffClass);
target.attr('class', cssOnClass);
}
         var setTokenName = target.data('set-token');
         if (setTokenName) {
             setToken(setTokenName, target.data('value'));
         }
         var unsetTokenName = target.data('unset-token');
         if (unsetTokenName) {
    var tokens = unsetTokenName.split(",");
var arrayLength = tokens.length;
for (var i = 0; i < arrayLength; i++) {
   setToken(tokens[i], undefined);
    //Do something
}      
             //setToken(unsetTokenName, undefined);
         }
         var tokenJson = target.data('token-json');
         if (tokenJson) {
             try {
                 if (_.isObject(tokenJson)) {
                     _(tokenJson).each(function(value, key) {
                         if (value == null) {
                             // Unset the token
                             setToken(key, undefined);
                         } else {
                             setToken(key, value);
                         }
                     });
                 }
             } catch (e) {
                 console.warn('Cannot parse token JSON: ', e);
             }
         }
     });
 });

 

Labels (1)
0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

@timgren 

Just remove  util/console  and console form the require block.

like

require(['jquery', 'underscore', 'splunkjs/mvc'], function($, _, mvc) {

 

You can directly use console  object in your JS code.

 

Full code.

require(['jquery', 'underscore', 'splunkjs/mvc'], function($, _, mvc) {
    console.log("hieeeee");
    function setToken(name, value) {
        var defaultTokenModel = mvc.Components.get('default');
        if (defaultTokenModel) {
            defaultTokenModel.set(name, value);
        }
        var submittedTokenModel = mvc.Components.get('submitted');
        if (submittedTokenModel) {
            submittedTokenModel.set(name, value);
        }
    }

// Main

    $('.dashboard-body').on('click', '[data-on-class],[data-off-class],[data-set-token],[data-unset-token],[data-token-json]', function(e) {
        e.preventDefault();

console.log("Inside the click");
        var target = $(e.currentTarget);
console.log("here");
console.log("target.data('on-class')=" + target.data('on-class'));

var cssOnClass= target.data('on-class');
var cssOffClass = target.data('off-class');

if (cssOnClass) {
$("." + cssOnClass).attr('class', cssOffClass);
target.attr('class', cssOnClass);
}
        var setTokenName = target.data('set-token');
        if (setTokenName) {
            setToken(setTokenName, target.data('value'));
        }
        var unsetTokenName = target.data('unset-token');
        if (unsetTokenName) {
   var tokens = unsetTokenName.split(",");
var arrayLength = tokens.length;
for (var i = 0; i < arrayLength; i++) {
  setToken(tokens[i], undefined);
   //Do something
}      
            //setToken(unsetTokenName, undefined);
        }
        var tokenJson = target.data('token-json');
        if (tokenJson) {
            try {
                if (_.isObject(tokenJson)) {
                    _(tokenJson).each(function(value, key) {
                        if (value == null) {
                            // Unset the token
                            setToken(key, undefined);
                        } else {
                            setToken(key, value);
                        }
                    });
                }
            } catch (e) {
                console.warn('Cannot parse token JSON: ', e);
            }
        }
    });
});

I hope this will help you.

Thanks
KV
An upvote would be appreciated if any of my replies help you solve the problem or gain knowledge.

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...

Modernize your Splunk Apps – Introducing Python 3.13 in Splunk

We are excited to announce that the upcoming releases of Splunk Enterprise 10.2.x and Splunk Cloud Platform ...

Step into “Hunt the Insider: An Splunk ES Premier Mystery” to catch a cybercriminal ...

After a whole week of being on call, you fell asleep on your keyboard, and you hit a sequence of buttons that ...