Functions Association

Hello everyone,

I would like to associate two functions in a 1 fuction (association) to store them in my database (MongoDB)


.

My first fuction is :

var dateActuelle = new Date();
var dateFormatee = dateActuelle.getFullYear() + "-" + (dateActuelle.getMonth() + 1) + "-" + dateActuelle.getDate() + " " + dateActuelle.getHours() + ":" + dateActuelle.getMinutes() + ":" + dateActuelle.getSeconds();
msg.payload = dateFormatee;
return msg;

My second fuction is :

var valeurOPCUA = msg.payload; // Stocke la valeur OPC UA dans une variable
flow.set('valeurOPCUA', valeurOPCUA); // Stocke la valeur dans une variable de flux (flow)
return msg;

Function Association :

var valeurOPCUA = flow.get('valeurOPCUA'); // Récupère la valeur OPC UA depuis la variable de flux
var dateFormatee = flow.get('dateFormatee'); // Récupère le timestamp depuis le message
var donnéesÀEnregistrer = {
valeurOPCUA: valeurOPCUA,
dateFormatee: dateFormatee
};
msg.payload = donnéesÀEnregistrer;
return msg;

But when i show my database,I don't understand why my values aren't stored in separate columns on the same rows.

Thanks for your helpfull
Léo

This is because Node-RED is message based and asynchronous. So at the start of your flow, you have 2 wires coming out of the trigger node. This creates two separate messages that flow independently of each other. Where you are feeding the 2 wires into a single node, those inputs are not coordinated, each wire results in a separate execution of the function node for each message on each wire.

Ideally, try to keep your flows linear and add additional properties on the msg that flow through to the end.

If that is not possible, you need to use a context variable to temporarily store things so that you can merge the values.

Add the first function code (or similar) into the second function rather than a separate parallel function.

Thanks for your feedback;

I try to create linear flow like this :

But my value is date is ever "null" on my database.

Thanks

And also i try, but doesn't works

date is undefined why ?

I think the problem is that in the last function you get dateFormatee from flow context, but you never actually set it on the context.

You can check the data stored in the context via the Context Data tab:

image

Add debug nodes on the Counter output and at each function output and check where the code is failing.

Hello guys,

I found my problem :slight_smile: !!!!!

Thanks for your helpfull,

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.