Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

failed with error: Javascript runtime error: "null"

Hi team facing issue with below javascript code

 

// get the version from request header + URI
var targetUrl = context.getVariable("target.url");
var pathSuffix = context.getVariable('proxy.pathsuffix');
var reqVersion = "EC-Version";

function orders() {
    return pathSuffix.includes("/order");
}
function payments() {
    return pathSuffix.includes("/pay");
}

var conditions = [
    { condition: orders, value: "orders" },
    { condition: payments, value: "payments.ecommerce" }
];

var matchingCondition = conditions.find(cond => cond.condition());
var requestPath = matchingCondition && matchingCondition.value || null;

// if I comment below line, and uncomment 2 lines then it works
var versionsMap = Object.keys(properties).map(key => [key, properties[key]]);

// var properti=JSON.parse(JSON.stringify(properties));
// const versionsMap = Object.keys(properti).map(key => [key, properti[key]]);

function getVersionForKey(key) {
  for (var i = 0; i < versionsMap.length; i++) {
    var entry = versionsMap[i];
    var entryKey = entry[0];
    var version = entry[1];
    if (entryKey === key) {
      return version;
    }
  }
  return null; // Return null if the key is not found
}
print(reqVersion);
context.setVariable("redirect.version",getVersionForKey(reqVersion));
context.setVariable("request.path", requestPath);

 

Harshad_Kadam_0-1700895267544.png

please let me know If I am doing anything wrong with that line, the error message seems common/generic.

Solved Solved
0 2 411
1 ACCEPTED SOLUTION

I think properties is not a JS native object, but a scriptable Java object that gets injected into the JS context .Therefore the Object.keys() does not work, but if you stringify and then parse, it does work.  

But it seems your code is working, right?  so all good?

View solution in original post

2 REPLIES 2

I think properties is not a JS native object, but a scriptable Java object that gets injected into the JS context .Therefore the Object.keys() does not work, but if you stringify and then parse, it does work.  

But it seems your code is working, right?  so all good?

Yes, thanks for the response. At first glance, I didn't understand, but after a bit of digging, I understood well. This graphic made simpler for me to relate.

Harshad_Kadam_0-1701435293920.png