cant check if variable is 'undefined'

Not applicable

I'm trying to retrieve a header valye 'idempotency_key'. The problem is that when 'idempotency_key' isn't passed. i get its value as undefined, but can't really check it in an 'if' condition.

var idempotency_key = request.headers['idempotency_key'];
print("idempotency_key1:" +JSON.stringify(idempotency_key)); // prints undefined
print("type:" +typeof idempotency_key);         // prints'object'
print("1:" +idempotency_key == "");             // false
print("2:" +idempotency_key === undefined);     // false
print("messageid:" +context.getVariable("messageid")); 
if(!idempotency_key  || idempotency_key == undefined){
  // do A  // never enters this code
}else{
  // do B
}
1 1 1,782
1 REPLY 1

@yuri abaev ,

Below code should work,

var idempotency_key = context.getVariable("request.header.idempotency_key");
if (idempotency_key == null) {
    print("Header missing");
} else {
    print("Header present");
}