Database Values as NodeJS Parameters

Post Reply
May
Newbie
Posts: 5
Joined: Thu May 25, 2023 1:05 pm

Database Values as NodeJS Parameters

Post by May »

We use a SwitchScript to save values in private data fields of the job and this script works perfectly well as a legacy script.

We now rewrote it as a NodeJS SwitchScript and it also works like a charm, but NOT when a parameter value for the script comes from a variable that fetches a value from a Database Connection like this:

Code: Select all

[Database.Text:SQL="SELECT #columnName FROM #tableName WHERE #columnValue = '[Job.PrivateData:Key="adID"]'",Connection="FileMaker-Database"]
(the hashtags are placeholders for privacy reasons, this part is just an example and works 100%)

The NodeJS SwitchScript fetches the parameters like this:

Code: Select all

tempKey = (await flowElement.getPropertyStringValue("key1")) as string;
tempValue = (await flowElement.getPropertyStringValue("value1")) as string;
ALL parameters that I pass to this script work fine, except those that are being read from an external database.

Ofcource, when I view the value of exactly this variable within Switch it can be read from the database within a second, so I get the correct values. But the NodeJS script gets passed an empty string in all these cases. I assume that the NodeJS script does not await the final value, even though there are "awaits" right before the flowElement.getPropertyStringValue() calls.

I even forced the script to wait 20 seconds right at the start, but this didn't help either. (Wouldn't be a solution anyway, because I can't just make all jobs wait. But it was worth a test:

Code: Select all

import { setTimeout } from "timers/promises";

await setTimeout(20000);
await job.log(LogLevel.Info, "waited 20 seconds... maybe we get all database values now?");
But as I said: This waiting didnt help either. I am quite sure that the NodeJS script gets empty strings even though the Database-Connection variables should have real values.

Do I miss anything?
Can anybody give advice?

Thank you very much
Daniel
User avatar
foxpalace
Member
Posts: 33
Joined: Fri Jan 14, 2011 12:25 pm
Location: Germany

Re: Database Values as NodeJS Parameters

Post by foxpalace »

Hi,

Code: Select all

[Database.Text:SQL="SELECT #columnName FROM #tableName WHERE #columnValue = '[Job.PrivateData:Key="adID"]'",Connection="FileMaker-Database"]
should it not be:

Code: Select all

[Database.Text:SQL="SELECT " + columnName + " FROM " + tableName + " WHERE " + columnValue + " = '[Job.PrivateData:Key="adID"]'",Connection="FileMaker-Database"]
May
Newbie
Posts: 5
Joined: Thu May 25, 2023 1:05 pm

Re: Database Values as NodeJS Parameters

Post by May »

The SQL SELECT works within the Switch Flow and the # hashtags are just placeholders as I wrote:

(the hashtags are placeholders for privacy reasons, this part is just an example and works 100%)
May
Newbie
Posts: 5
Joined: Thu May 25, 2023 1:05 pm

Re: Database Values as NodeJS Parameters

Post by May »

We experience this even more often. Using variables from "a Database Connection" (via the Database Connection Module) as parameters for scripts seems to be broken.

I don't know if this is something that only we experience?

If anybody else can confirm this "behavior", this might be worth a serious bug report?
tdeschampsBluewest
Member
Posts: 34
Joined: Tue Jun 01, 2021 11:57 am

Re: Database Values as NodeJS Parameters

Post by tdeschampsBluewest »

From what you describe, it's look like the "await" does not really await for the result.

Could you try something like this? (I know, it's not perfect, but it should work)

Code: Select all


function sleep(ms: number) {
	return new Promise((resolve) => setTimeout(resolve, ms));
}

let tempKey:null|string = null
tempKey = (await flowElement.getPropertyStringValue("key1")) as string;


let increment: number = 10;
let timeElapsed = 0;
let tempoMax = 5000
while (tempKey === null) {
    await sleep(increment);
    if (timeElapsed > tempoMax) {
        throw new Error(`timeout have been reached ( ${tempoMax} ms)`);
        break;
    }
    timeElapsed += increment;
}

May
Newbie
Posts: 5
Joined: Thu May 25, 2023 1:05 pm

Re: Database Values as NodeJS Parameters

Post by May »

I highly appreciate your help, Thomas.

When I try your code, I get:

Code: Select all

Argument of type '(value: unknown) => void' is not assignable to parameter of type 'number'.
because the setTimeout returns void and something else is expected?

I was eager enough to "just remove" the _resolve_ keyword/parameter of the setTimeout function:

Code: Select all

async function sleep(ms: number) {
	return new Promise((resolve) => {
		return setTimeout(ms);
	});
}
Can someone with more TypeScript knowledge estimate what this means?
Does the code actually wait for the promise?
Or does removing this _resolve_ destroy the logic of the promise?

*EDIT*:

I did some more testing and so far it seemed to work. I felt "unconfident" about this and did some more testings, see next post.

To be honest @Enfocus: This should not be necessary at all, cause the script should not even start before its parameters are present?
Last edited by May on Thu Jun 29, 2023 10:07 am, edited 1 time in total.
May
Newbie
Posts: 5
Joined: Thu May 25, 2023 1:05 pm

Re: Database Values as NodeJS Parameters

Post by May »

Sad story.

It only works when the parameters come back fast enough. I penetrated it with ~20 switch jobs sequentially (not even parallel) and all those jobs had the same parameters being read from a database. Don't get me wrong, but 20 test jobs is not even much, this would definitely happen in production. Most of the jobs work, but on some jobs I get the following errors in my switch messages:

Code: Select all

Initialisierung der Parameter fehlgeschlagen
Befehl kann nicht gelesen werden: EvaluateProperties
in english:

Code: Select all

initializing parameters failed
command cannot be read: EvaluateProperties
which are both switch internal error messages and not logged by my script.

This REALLY feels like I can't fix this within the Node.JS-Switch-Script
Padawan
Advanced member
Posts: 358
Joined: Mon Jun 12, 2017 8:48 pm
Location: Belgium
Contact:

Re: Database Values as NodeJS Parameters

Post by Padawan »

Then I think the best option is to have some small reproducible sample and report it to Enfocus Support.

Another workaround you could try: put the requesting of the property in a try catch, and if there is an exception, wait for 1 second or so and then request the property again.

Even if the workaround works I would strongly advise to report it to Enfocus support. Nobody else here responded that they already noticed the behavior, so most likely it is not reported to Enfocus yet. If it is not reported, then it can't be fixed.
Post Reply