Page 1 of 1

Undefined is not a function

Posted: Mon Apr 14, 2025 11:20 pm
by smckinney
Hello everyone,

I'm having an issue with my script. I'm honestly just running test scripts but regardless if I'm putting in one that I thought would work or the actual script I want to be apart of the flow, I'm still running into the same error: "undefined is not a function." I've also tried the async version too and that also returned the exact same error. Can someone help me PLEASE because this is driving me insane!

"var fs = require("fs");

function jobArrived(s, flowElement, job) {
try {
s.log(1, "Script loaded successfully.");

var inputPath = job.getPath();

s.log(1, "Input path: " + inputPath);

var outputPath = inputPath + ".txt";

fs.writeFileSync(outputPath, "Hello from Classic Switch Script!");

job.sendToSingle(outputPath);

} catch (err) {
s.log(1, "Full error: " + JSON.stringify(err));
job.fail("Test script failed: " + err.message);
}
}"

Re: Undefined is not a function

Posted: Tue Apr 15, 2025 9:17 am
by freddyp
What you have been trying to do is write a NodeJS script as if it were a legacy script, but these two environments are quite different.

Code: Select all

s.log(1, "Script loaded successfully");
becomes

Code: Select all

await flowElement.log(LogLevel.Info, "Script loaded successfully");
That is why you get the undefined error because the Switch class no longer has a method to log messages. It is either on the FlowElement or Job class that you log messages. When a Job instance is available it is better practice to use that one, so the line above is better like this:

Code: Select all

await job.log(LogLevel.Info, "Script loaded successfully");
Sending jobs also works differently. I am not going to explain that here. Please follow the scripting trail on the Learn section:
https://www.enfocus.com/en/learn/switch

Re: Undefined is not a function

Posted: Tue Apr 15, 2025 3:10 pm
by smckinney
Oh my gosh, you are a lifesaver thank you!! This was killing me! Also thank you for the link, I've been looking for something like this!!