Skip to content

Instantly share code, notes, and snippets.

@jlbruno
Last active March 22, 2024 18:36
Show Gist options
  • Save jlbruno/decf823aa7e60a3e210db0a50e34dd34 to your computer and use it in GitHub Desktop.
Save jlbruno/decf823aa7e60a3e210db0a50e34dd34 to your computer and use it in GitHub Desktop.
SFCC object debug - Useful for debugging pdict
'use strict';
/**
* A place to debug an object from an isml file
* Useful to pass in pdict to see what it contains
*
* @param debugObject Object you'd like to see in the debugger
* @param debugLabel Optional label for the debugObject
*
**/
function debug(debugObject, debugLabel) {
// debugObject and debugLabel will show in debugger
return true; // put breakpoint here
}
/**
* Allows you to console.log from within a backend script or isscript block
* Exposes some of the built in console logging methods
* Pass in as many arguments as you'd like, they'll get output in a comma delimted list
*/
var console = function() {
var str = function(args) {
var temp = '';
// loop through arguments and add argument to string
for (var i = 0, j = args.length; i < j; i++){
var comma = ((i === 0) ? '' : ', ');
temp = temp + comma + args[i];
}
return temp;
}
function log() {
var string = "<script>console.log('" + str(arguments) + "');</script>";
response.writer.print(string);
}
function debug() {
var string = "<script>console.debug('" + str(arguments) + "');</script>";
response.writer.print(string);
}
function info() {
var string = "<script>console.info('" + str(arguments) + "');</script>";
response.writer.print(string);
}
function warn() {
var string = "<script>console.warn('" + str(arguments) + "');</script>";
response.writer.print(string);
}
function error() {
var string = "<script>console.error('" + str(arguments) + "');</script>";
response.writer.print(string);
}
function group() {
var string = "<script>console.group('" + str(arguments) + "');</script>";
response.writer.print(string);
}
function groupEnd() {
var string = "<script>console.groupEnd('" + str(arguments) + "');</script>";
response.writer.print(string);
}
return {
log: log,
debug: debug,
info: info,
warn: warn,
error: error,
group: group,
groupEnd: groupEnd
}
}();
module.exports = {
debug: debug,
console: console
}
<isscript>
var Debug = require('*/cartridge/scripts/util/DebugHelper');
// pass in an object to see it in the debugger when running a debug session
Debug.debug(pdict, "pdict");
// call the console methods to output a value to your browser's console
Debug.console.log('product name', pdict.product.name);
</isscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment