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
Get Updates on the Splunk Community!

Splunk + ThousandEyes: Correlate frontend, app, and network data to troubleshoot ...

 Are you tired of troubleshooting delays caused by siloed frontend, application, and network data? We've got a ...

Splunk Observability for AI

Don’t miss out on an exciting Tech Talk on Splunk Observability for AI!Discover how Splunk’s agentic AI ...

🔐 Trust at Every Hop: How mTLS in Splunk Enterprise 10.0 Makes Security Simpler

From Idea to Implementation: Why Splunk Built mTLS into Splunk Enterprise 10.0  mTLS wasn’t just a checkbox ...