. Need help with Retrieving json from Gcp Bucket through Apigee using js file to remove new lines,\

My js file is below :

var retreivedStorageFile = context.getVariable("storage.file.retrieved");
var val = retreivedStorageFile.replace(/\\/g, " ");
print("retreivedStorageFile is: " + val);

But above code gives : 

LakshmiPavitra_0-1660942806307.png

0 1 62
1 REPLY 1

You have two problems

  1. the error message says "cannot call method "replace" of null. That means the object, on which you are attempting to invoke the .replace() method, is a null value. In your code that is retreivedStorageFile, where you use it on line 2. That object is set in the line above - line 1. Via the context.getVariable() call. Which tells us that the named variable does not exist or holds a null value. It's not set. To protect against this you may want to insert a check for null in your JS code. something like this:

    var retrievedFile = context.getVariable('my-variable-name');
    if (retrievedFile) { 
      // do something with it
    }
    else {
      // the context variable is not set
      print("context variable is not set.");
      // probably here you want to throw an exception? 
      throw new Error("unexpected: context variable is not set.");
    }
  2. You are trying to replace double slashes. That's probably unnecessary. See my answer to your other question. You don't need to call .replace() at all.