Home > Software design >  "string.split is not a function" when called via a web request, but works as expected in A
"string.split is not a function" when called via a web request, but works as expected in A

Time:10-05

EDIT: Solved, but posting anyway because I want to understand what's happening.

I'm developing a project in Google Apps Script and Powershell to assist in creating customized Powershell configuration scripts for mass-configuration of Windows machines.

When testing the code in the Apps Script web IDE, the string is split properly. However, when calling the script through Powershell's Invoke-Webrequest cmdlet, the result I'm getting from Google contains an error saying that .split is not a function.

Powershell code:

$queryString = "files=$query" # query is a string of google file IDs joined by commas
$server = "https://script.google.com/macros/s/google_script_id/exec?"
$request = $server   $queryString
try {
    $Response = Invoke-WebRequest $request -UseBasicParsing
} catch {
    Write-Host "Failed to complete the web request.  Trying again in 5 seconds..."

Google Apps Script code:

let content = "";
let filestr = e.parameters.files; //e.parameters.files is the string of file IDs joined by commas, passed by Powershell
console.log(typeof filestr); //logs type as string, see below
let files = filestr.split(",");
for (let i = 0; i < files.length; i  ) {
  try {
    let script = DriveApp.getFileById(files[i])
    content  = script.getAs("text/plain");
    content  = "::"
  } catch (ex) {
    message = `Failed::${ex.name}:${ex.message}`;
    return message;
  }
}
message = `SUCCESS::${content}`;
return message;

Picture showing that filestr is a string with console.log(typeof filestr)

HTML error I'm getting in my Powershell script (filestr.split is not a function):

"<!DOCTYPE html><html><head><link rel="shortcut icon" href="//ssl.gstatic.com/docs/script/images/favicon.ico"><title>Error</title><style type="text/css" nonce="1fq8pQfEFhMLUJvDdsXUDw">body {background-color: #fff; margin: 0; padding: 0;}.errorMessage {font-family: Arial,sans-serif; font-size: 12pt; font-weight: bold; line-height: 150%; padding-top: 25px;}</style></head><body style="margin:20px"><div><img alt="Google Apps Script" src="//ssl.gstatic.com/docs/script/images/logo.png"></div><div style="text-align:center;font-family:monospace;margin:50px auto 0;max-width:600px">TypeError: filestr.split is not a function (line 117, file &quot;Code&quot;)</div></body></html>"

Like I said, the script processes normally in the Apps Script IDE, but for some reason I get an error when Powershell calls it via a web request. Hopefully this post is pointless and it's an easy solve, but lots of the questions I've seen so far have involved people calling .split on object properties or other things that they think are strings, but actually aren't. I'm at least fairly confident that I'm calling this method on an actual string though, and the fact that it works sometimes and not others is puzzling. Any insight would be appreciated.

Solution: I changed the line let filestr = e.parameters.files; to let filestr = new String(e.parameters.files);, and now I'm getting the value I expect in Powershell.

After typing this up, I went to have a last look around the web, and found a Stack Overflow post that mentioned that when an object method is called on a primitive value, a temporary object is created to house the value so that the object method can be run. Afterwards the object is discarded. Is this process somehow to blame for the behavior I experienced, or is there some other reason why creating an object first worked?

SO answer that gave me the inspiration to create a wrapper object: How is a Javascript string not an object?

CodePudding user response:

Per suggestions from the comments, I'm posting the solution I found as an answer for clarity.

Solution: I changed the line let filestr = e.parameters.files; to let filestr = new String(e.parameters.files);, and am now able to call .split() on the string without getting an error in Powershell.

  • Related