
The error I'm getting is:
undefined is not a function TypeError: undefined is not a function at jobArrived (C:\Users\enfocusswitch\AppData\Roaming\Enfocus\SwitchProcessorService\cache\7ce230b50ef35d1b9adecf311dd1baa99d4fdea10c8394276d9b97258fa46f44\1731425389\main.js:1:3647) at _0xbe11ff (C:\Program Files\Enfocus\Enfocus Switch\ScriptExecutor\nodemodules\node_modules\switch-scripting\index.js34) at C:\Users\enfocusswitch\AppData\Roaming\Enfocus\SwitchProcessorService\cache\7ce230b50ef35d1b9adecf311dd1baa99d4fdea10c8394276d9b97258fa46f44\1731425389\main.js:1:5847 at AsyncResource.runInAsyncScope (node:async_hooks:201:9) at C:\Users\enfocusswitch\AppData\Roaming\Enfocus\SwitchProcessorService\cache\7ce230b50ef35d1b9adecf311dd1baa99d4fdea10c8394276d9b97258fa46f44\1731425389\main.js:1:5504 at new Promise (<anonymous>) at entrypointWrapper (C:\Users\enfocusswitch\AppData\Roaming\Enfocus\SwitchProcessorService\cache\7ce230b50ef35d1b9adecf311dd1baa99d4fdea10c8394276d9b97258fa46f44\1731425389\main.js:1:5321) at C:\Users\enfocusswitch\AppData\Roaming\Enfocus\SwitchProcessorService\cache\7ce230b50ef35d1b9adecf311dd1baa99d4fdea10c8394276d9b97258fa46f44\1731425389\main.js:1:7539 at _0x1a94d4.run (C:\Program Files\Enfocus\Enfocus Switch\ScriptExecutor\NodeScriptExecutor.js:429:30) at async process.<anonymous> (C:\Program Files\Enfocus\Enfocus Switch\ScriptExecutor\NodeScriptExecutor.js:280:41)
I used the Switch Scripter tool to create my directory structure, I then programmed everything in my main.ts file and configured other files as necessary. In my SSCRIPT, I linked to the main.ts file that I put my code into. The script is designed to read an XML file, see if a field has a _1 or something else and, depending on that field, is supposed to send the file down a specific path.
Here's the code for my main.ts file:
Code: Select all
/// <reference types="switch-scripting" />
import { readFile } from 'fs/promises';
import { XMLParser } from 'fast-xml-parser';
export async function jobArrived(flowElement: FlowElement, job: Job) {
try {
// Retrieve the dataset
const dataset = await job.getDataset('Processed DSF JDF', AccessLevel.ReadOnly);
if (!dataset) {
await job.log(LogLevel.Warning, "Dataset 'Processed DSF JDF' not found");
await job.sendToSingle("2");
return;
}
// Access the dataset file path
const datasetPath = await job.get(AccessLevel.ReadOnly);
if (!datasetPath) {
await job.log(LogLevel.Warning, 'Unable to access dataset file');
await job.sendToSingle("2");
return;
}
// Read and parse the dataset XML
const xmlContent = await readFile(datasetPath, 'utf-8');
const parser = new XMLParser({ ignoreAttributes: false });
const xmlData = parser.parse(xmlContent);
// Navigate the parsed XML to get the UserFileName attribute
const jdf = xmlData['dn:JDF']?.['dn:JDF']?.['dn:ResourcePool']?.['dn:RunList']?.['dn:RunList']?.['dn:LayoutElement']?.['dn:FileSpec'];
const unparsedJobNumber = jdf?.['@_UserFileName'];
if (!unparsedJobNumber) {
await job.log(LogLevel.Warning, 'Unable to retrieve job number from metadata');
await job.sendToSingle("2");
return;
}
const endPosition = unparsedJobNumber.toUpperCase().indexOf('.PDF');
if (endPosition === -1) {
await job.log(LogLevel.Warning, 'Job number format is incorrect');
await job.sendToSingle("2");
return;
}
const cleanedJobNumber = unparsedJobNumber.substring(0, endPosition);
const parts = cleanedJobNumber.split('_');
if (parts.length < 2) {
await job.log(LogLevel.Warning, 'Job number format is incorrect');
await job.sendToSingle("2");
return;
}
const externalSubJobID = parts[1];
// Send job to the appropriate output based on the sub-job ID
if (externalSubJobID === '1') {
await job.log(LogLevel.Info, `Job '${await job.getName()}' sent to path 1`);
await job.sendToSingle("1");
} else {
await job.log(LogLevel.Info, `Job '${await job.getName()}' sent to path 2`);
await job.sendToSingle("2");
}
} catch (error) {
await job.fail(`Job processing failed due to an error: ${(error as Error).message}`);
}
}
Here is the main.js code that's created when I run the transpile command:
Code: Select all
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.jobArrived = void 0;
/// <reference types="switch-scripting" />
const promises_1 = require("fs/promises");
const fast_xml_parser_1 = require("fast-xml-parser");
async function jobArrived(flowElement, job) {
var _a, _b, _c, _d, _e, _f;
try {
// Retrieve the dataset
const dataset = await job.getDataset('Processed DSF JDF', AccessLevel.ReadOnly);
if (!dataset) {
await job.log(LogLevel.Warning, "Dataset 'Processed DSF JDF' not found");
await job.sendToSingle("2");
return;
}
// Access the dataset file path
const datasetPath = await job.get(AccessLevel.ReadOnly);
if (!datasetPath) {
await job.log(LogLevel.Warning, 'Unable to access dataset file');
await job.sendToSingle("2");
return;
}
// Read and parse the dataset XML
const xmlContent = await (0, promises_1.readFile)(datasetPath, 'utf-8');
const parser = new fast_xml_parser_1.XMLParser({ ignoreAttributes: false });
const xmlData = parser.parse(xmlContent);
// Navigate the parsed XML to get the UserFileName attribute
const jdf = (_f = (_e = (_d = (_c = (_b = (_a = xmlData['dn:JDF']) === null || _a === void 0 ? void 0 : _a['dn:JDF']) === null || _b === void 0 ? void 0 : _b['dn:ResourcePool']) === null || _c === void 0 ? void 0 : _c['dn:RunList']) === null || _d === void 0 ? void 0 : _d['dn:RunList']) === null || _e === void 0 ? void 0 : _e['dn:LayoutElement']) === null || _f === void 0 ? void 0 : _f['dn:FileSpec'];
const unparsedJobNumber = jdf === null || jdf === void 0 ? void 0 : jdf['@_UserFileName'];
if (!unparsedJobNumber) {
await job.log(LogLevel.Warning, 'Unable to retrieve job number from metadata');
await job.sendToSingle("2");
return;
}
const endPosition = unparsedJobNumber.toUpperCase().indexOf('.PDF');
if (endPosition === -1) {
await job.log(LogLevel.Warning, 'Job number format is incorrect');
await job.sendToSingle("2");
return;
}
const cleanedJobNumber = unparsedJobNumber.substring(0, endPosition);
const parts = cleanedJobNumber.split('_');
if (parts.length < 2) {
await job.log(LogLevel.Warning, 'Job number format is incorrect');
await job.sendToSingle("2");
return;
}
const externalSubJobID = parts[1];
// Send job to the appropriate output based on the sub-job ID
if (externalSubJobID === '1') {
await job.log(LogLevel.Info, `Job '${await job.getName()}' sent to path 1`);
await job.sendToSingle("1");
}
else {
await job.log(LogLevel.Info, `Job '${await job.getName()}' sent to path 2`);
await job.sendToSingle("2");
}
}
catch (error) {
await job.fail(`Job processing failed due to an error: ${error.message}`);
}
}
exports.jobArrived = jobArrived;
//# sourceMappingURL=main.js.map
Again, I'm new to this because I've been using Legacy Coding for years and now have to make the switch to Node.JS.
What am I missing? If you need more information or need to see code from other files, let me know. Also attached is a screenshot of my folder structure inside of VS Code.