How to overcome JS policy execution error ?

So this is the error i am receiving

{
    "fault": {
        "faultstring": "Execution of JS.ExpiryTime failed with error: Javascript runtime error: \"TypeError: Cannot find function setvariable in object context;flow=PROXY_REQ_FLOW;session={}. (Expiry.js:16)\"",
        "detail": {
            "errorcode": "steps.javascript.ScriptExecutionFailed"
        }
    }
}
var expiration = context.getVariable("req.expiry");
print (expiration);
var str = expiration.toString();
print(str);
var a = str.search(" ");
var dateold =str.slice(0, a);
print (dateold);
context.setvariable("datenew",dateold);
print(datenew);
var timefull =str.slice(a+1);
var time2 =timefull.split("+");
var time1 =time2[0];
context.setvariable("timenew",time1);
print (time1);
var zone= time2[1];
print(zone);


So i have this expiry date and time coming in which i have split and save them date and time respectively. I have gone through this link

https://docs.apigee.com/api-platform/troubleshoot/policies/runtime/javascript-runtime-errors but i could not what is wrong in my code and why the variable is not set .

Solved Solved
0 6 1,073
1 ACCEPTED SOLUTION

Try context.setVariable

Note, it's case sensitive

You can see JavaScript object model here https://docs.apigee.com/api-platform/reference/javascript-object-model#methods

View solution in original post

6 REPLIES 6

sujnana
Participant IV

I think print statement is not allowed in JS policy. You can use console.log.

Ex: console.log(expiration);

In the JS for JS callout policies in Apigee, print is allowed; console.log() is not.

In nodejs, it's the reverse.

This can be confusing, but there it is.

Hi Dino,

yesterday everything worked but i do not know it is strange i am getting error again.

//var payload = context.getVariable("calloutResponse");
//print (payload);
/*var jsonObject= JSON.parse(payload);
print (jsonObject);*/
//var otp = jsonObject.users.expires_after;
var expiration = context.getVariable("req.expiry");
print (expiration);
//var d = "2021-06-08 06:24:24+00:00";
var str = expiration.toString();
print(str);
var a = str.search(" ");
var dateold =str.slice(0, a);
//print (dateold);
//var datenew=context.setVariable("datenew",dateold);
context.setVariable("datenew",dateold);
print(datenew);
var timefull =str.slice(a+1);
var time2 =timefull.split("+");
var time1 =time2[0];
context.setVariable("timenew",time1);
print (time1);
var zone= time2[1];
print(zone);



{
    "fault": {
        "faultstring": "Execution of JS.ExpiryTime failed with error: Javascript runtime error: \"ReferenceError: \"datenew\" is not defined. (Expiry.js:16)\"",
        "detail": {
            "errorcode": "steps.javascript.ScriptExecutionFailed"
        }
    }
}


variable not defined but datenew was defined right?

If you read the error messages and refer to the code you should be able to easily identify these issues

Where you define the variable, it is commented out although I'm not sure what value you're expecting it to have as you assigned it to context.setVariable(..)...

Additionally, mixing and matching variable names make your code less readable (eg you have the dateold variable, yet you're using it to assign in the context a datenew variable)

Try context.setVariable

Note, it's case sensitive

You can see JavaScript object model here https://docs.apigee.com/api-platform/reference/javascript-object-model#methods

Silly mistake it is working now.