Debugging Server Side External Sisense Plugins using HTTP Requests
One common issue when sending Sisense JS API objects such as the prism object, or a dashboard or widget object in the payload of a HTTP request, is that the object can be self-referential, making it challenging to convert to JSON in the usual manner. The following code can be used to remove circular references in JSON:
function refReplacer() {
let m = new Map(), v = new Map(), init = null;
return function (field, value) {
let p = m.get(this) + (Array.isArray(this) ? `[${field}]` : '.' + field);
let isComplex = value === Object(value)
if (isComplex) m.set(value, p);
let pp = v.get(value) || '';
let path = p.replace(/undefined\.\.?/, '');
let val = pp ? `#REF:${pp[0] == '[' ? '$' : '$.'}${pp}` : value;
!init ? (init = value) : (val === init ? val = "#REF:$" : 0);
if (!pp && isComplex) v.set(value, path);
return val;
}
}
To convert the JSON form of an object with circular references removed back into the object's original state, you can use the following function:
function parseRefJSON(json) {
let objToPath = new Map();
let pathToObj = new Map();
let o = JSON.parse(json);
let traverse = (parent, field) => {
let obj = parent;
let path = '#REF:$';
if (field !== undefined) {
obj = parent[field];
path = objToPath.get(parent) + (Array.isArray(parent) ? `[${field}]` : `${field?'.'+field:''}`);
}
objToPath.set(obj, path);
pathToObj.set(path, obj);
let ref = pathToObj.get(obj);
if (ref) parent[field] = ref;
for (let f in obj) if (obj === Object(obj)) traverse(obj, f);
}
traverse(o);
return o;
}
Below is an example of sending a variable with circular references removed:
You can define this function once and use it in multiple parts of your code for debugging.
Another example, incorporating the functions mentioned above, is shown below, where jQuery Ajax functionality is used, and circular references to a Sisense dashboard variable are removed:
let debugVariable = [JSON.parse(JSON.stringify(prism.activeDashboard, refReplacer()))]
$.ajax({
type: "POST",
url: "https://webhook.site/your-Unique-Webhook-URL-or-any-server-with-viewable-logs",
crossDomain: true,
data: JSON.stringify({ debugVariable: debugVariable }),
success: function (data) {
},
error: function (err) {
}
});
Comment your experience with this, we'd love to start this discussion!