Page 1 of 1

Private Data not being set

Posted: Thu Jun 01, 2023 10:04 am
by JimmyHartington
I am beginning to migrate my old script elements to TypeScript.
They are simple scripts, but I have succeeded to migrate 3 without any problems. :D

But now I have run into a problem with the next one.
What I need to do is to read the contents of a CSV-file and map the different records to private data. It work fine in the other scripts I have migrated, but does not work here.

The contents of the file could be like this:

Code: Select all

DEU;<b>HTML</b>-line of text<br>which should be private data;
My code is below and I can log the data I need to become private data to messages, but it does not get set as private data.
I am probably missing something simple.

Image

Image

Code: Select all

import * as fs from "fs";

async function jobArrived(s: Switch, flowElement: FlowElement, job: Job) {

    let jobPath: string = await job.get(AccessLevel.ReadOnly) as string;
    let inputFileText = await fs.readFileSync(jobPath, "utf-8");

    await job.log(LogLevel.Info, "Filecontent: " + inputFileText);
    
    let arrData = await inputFileText.split("\n");
    await job.log(LogLevel.Info, "Antal sprog i CSV: " + arrData.length);
    
    for( let i =0;i < arrData.length; i++) {
		
		// Set to PD
		let arrDataLine = await arrData[i].split(";");
		let prefix = await arrDataLine[0];
                await job.log(LogLevel.Info, "Prefix: " + prefix);
                await job.log(LogLevel.Info, "Language: " + arrDataLine[0]);
                await job.log(LogLevel.Info, "HTML: " + arrDataLine[1]);
		await job.setPrivateData(prefix + "-strLanguage", arrDataLine[0]);
		await job.setPrivateData(prefix + "-strHTMLline", arrDataLine[1]);
		
	}

    await job.sendToData(Connection.Level.Success);
}

Re: Private Data not being set

Posted: Thu Jun 01, 2023 1:48 pm
by mkayyyy
I've tested your script and the example file you provided and the private data looks to be set correctly?

Image

Re: Private Data not being set

Posted: Thu Jun 01, 2023 6:08 pm
by JimmyHartington
Hmm. Curious. I must have done something wrong in the flow.
I will try to create a simple flow to test.
Perhaps something in my production-data messes something up.

Thanks for trying the script. :)

Re: Private Data not being set

Posted: Fri Jun 02, 2023 9:06 am
by JimmyHartington
I found out why this did not work.
The file I receive in production is UTF8 BOM.
So when I read the file in Node.JS the BOM character is read as well.
Then my first "record" starts with the BOM character.
In this case it is solved by adding ".trim()" to the code reading the file like this:

Code: Select all

    let inputFileText = await fs.readFileSync(jobPath, "utf-8").trim();
It probably worked for mkayyyy since the test-file was saved a UTF-8 without a BOM character.

The way I found this was by copying the log from Switch messages.
The BOM character is not shown in the log, but by pasting it into Visual Studio Code, I could see it.
Image

Maybe the log should show these characters as well?

Re: Private Data not being set

Posted: Fri Jun 02, 2023 12:01 pm
by mkayyyy
Ah that makes sense, I remember running into issues with UTF8 BOM whilst writing a script a few years ago. Although I can't remember now how exactly I resolved it haha

Re: Private Data not being set

Posted: Fri Jun 02, 2023 12:24 pm
by JimmyHartington
Programming is still new to me, so I suspect I will run into more cases like this.
But it is nice with a forum like this, where others input can help solve it. :D