# Functions Association

**URL:** <https://discourse.nodered.org/t/functions-association/81974>\
**Category:** General\
**Created:** [12 October 2023 15:44 UTC](https://discourse.nodered.org/t/functions-association/81974 "2023-10-12T15:44:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![leo17](https://avatars.discourse-cdn.com/v4/letter/l/e0b2c6/32.png) [@leo17](https://discourse.nodered.org/u/leo17)\
**Post date:** [12 October 2023 15:44 UTC](https://discourse.nodered.org/t/functions-association/81974/1 "2023-10-12T15:44:04Z")

</div>

Hello everyone,

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

 ![1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/a/4a9d3e31236dee3be932c08bb4d579bc2e7f967e.png)  
.

**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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/d/fdaf05ba3a8143e7e224e75576131d9e645ecb14.png)

Thanks for your helpfull  
Léo

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [12 October 2023 16:31 UTC](https://discourse.nodered.org/t/functions-association/81974/2 "2023-10-12T16:31:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [12 October 2023 17:01 UTC](https://discourse.nodered.org/t/functions-association/81974/3 "2023-10-12T17:01:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![leo17](https://avatars.discourse-cdn.com/v4/letter/l/e0b2c6/32.png) [@leo17](https://discourse.nodered.org/u/leo17)\
**Post date:** [13 October 2023 08:17 UTC](https://discourse.nodered.org/t/functions-association/81974/4 "2023-10-13T08:17:11Z")

</div>

Thanks for your feedback;

I try to create linear flow like this :

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/8/0854517ef7da9bdd96da85222113e14daf4e06bf.png)

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/3/837ff5cae477d8e70ed90410c47a90e598accf60.png)

Thanks

---

<div class="post-metadata">

**Author:** ![leo17](https://avatars.discourse-cdn.com/v4/letter/l/e0b2c6/32.png) [@leo17](https://discourse.nodered.org/u/leo17)\
**Post date:** [13 October 2023 08:25 UTC](https://discourse.nodered.org/t/functions-association/81974/5 "2023-10-13T08:25:29Z")

</div>

And also i try, but doesn't works

date is undefined why ?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/d/cd2023a6850c8e6776e491d1e4ef949cc844c7ec.png)

---

<div class="post-metadata">

**Author:** ![andrei-tatar](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei-tatar/32/3780_2.png) [@andrei-tatar](https://discourse.nodered.org/u/andrei-tatar)\
**Post date:** [13 October 2023 08:42 UTC](https://discourse.nodered.org/t/functions-association/81974/6 "2023-10-13T08:42:08Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/7/d78eaa59b5776989f23692cfce920c2fb2b41a81.png)

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

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/d/0da06f40fc4f819dfeecbc8908c6fcdb7188f8f8.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 October 2023 08:46 UTC](https://discourse.nodered.org/t/functions-association/81974/7 "2023-10-13T08:46:03Z")

</div>

> [@leo17](#):
>
> I try to create linear flow like this :

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

---

<div class="post-metadata">

**Author:** ![leo17](https://avatars.discourse-cdn.com/v4/letter/l/e0b2c6/32.png) [@leo17](https://discourse.nodered.org/u/leo17)\
**Post date:** [13 October 2023 13:17 UTC](https://discourse.nodered.org/t/functions-association/81974/8 "2023-10-13T13:17:32Z")

</div>

Hello guys,

I found my problem 🙂 !!!!!

Thanks for your helpfull,

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/3/f30c6698d5ab3bd20cd0ba927df1bc2acc3d71cb.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/5/85d48168ca482322ddfb5cdc74fc662f50efdfd7.png)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [27 October 2023 13:17 UTC](https://discourse.nodered.org/t/functions-association/81974/9 "2023-10-27T13:17:36Z")

</div>

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