# Question about JS function (return msg)

**URL:** <https://discourse.nodered.org/t/question-about-js-function-return-msg/52661>\
**Category:** General\
**Created:** [24 October 2021 14:21 UTC](https://discourse.nodered.org/t/question-about-js-function-return-msg/52661 "2021-10-24T14:21:52Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![xavier98](https://avatars.discourse-cdn.com/v4/letter/x/f08c70/32.png) [@xavier98](https://discourse.nodered.org/u/xavier98)\
**Post date:** [24 October 2021 14:21 UTC](https://discourse.nodered.org/t/question-about-js-function-return-msg/52661/1 "2021-10-24T14:21:52Z")

</div>

Hello to all,

I come from the world of PLC programming so I'm not comfortable with javascript yet haha. I'm making a connected bar with Node-RED.

I wanted to know where I should put "return msg (;)" in my code in order to return a msg. ... with conditions. Should I put it after each if ? or since it's already a function I should leave the return at the bottom ?  
I know that my code is not yet optimal but I would like to have everything working before optimizing it with a for loop.

thanks in advance

my code :

```auto
//Déclaration variables
var xStartCycle = false
const recette = []
var poidsactuel = flow.get("mesurepoids")
var EtapesCycle = 0

//Lancement cycle + tare balance + chargement recette 
if (msg.payload == true && xStartCycle == false){
    xStartCycle = true
    msg.tare = 1
    recette[1] = flow.get('rDosageLiquide1')
    recette[2] = flow.get('rDosageLiquide2')
    recette[3] = flow.get('rDosageLiquide3')
    recette[4] = flow.get('rDosageLiquide4')
    
} 

//Vérification balance = 0g et lancement des étapes du cycle
if (xStartCycle == true && poidsactuel < 1 ){
    
    msg.payload = 1
    msg.tare = 0
    
     
    } 

    //Etapes cycle
    if(EtapesCycle == 1){
       if (poidsactuel < recette[EtapesCycle] && poidsactuel < 1){
           msg.result = 1
           
       } else if (poidsactuel >= recette[EtapesCycle]){
           msg.result = 0
           EtapesCycle = EtapesCycle +1
           msg.tare = 1
           
       } else if (recette[EtapesCycle] == 0){
           EtapesCycle = EtapesCycle +1
           
       }
        
                        }
                        
   if(EtapesCycle == 2){
       if (poidsactuel < recette[EtapesCycle] && poidsactuel < 1){
           msg.result = 1
           
       } else if (poidsactuel >= recette[EtapesCycle]){
           msg.result = 0
           EtapesCycle = EtapesCycle +1
           msg.tare = 0
           
       } else if (recette[EtapesCycle] == 0){
           EtapesCycle = EtapesCycle +1
           
       }
        
                        }
    
    if(EtapesCycle == 3){
       if (poidsactuel < recette[EtapesCycle] && poidsactuel < 1){
           msg.result = 1
           
       } else if (poidsactuel >= recette[EtapesCycle]){
           msg.result = 0
           EtapesCycle = EtapesCycle +1
           msg.tare = 1
           
       } else if (recette[EtapesCycle] == 0){
           EtapesCycle = EtapesCycle +1
           
       }
        
                        }
                        
    if(EtapesCycle == 4){
       if (poidsactuel < recette[EtapesCycle] && poidsactuel < 1){
           msg.result = 1
           
       } else if (poidsactuel >= recette[EtapesCycle]){
           msg.result = 0
           xStartCycle = false
           msg.tare = 0
           
       } else if (recette[EtapesCycle] == 0){
           xStartCycle = false
       }
        
                        }
                        
    //Fin cycle
    if(EtapesCycle == 4 && xStartCycle == false){
        EtapesCycle = 0
    }
    
    return msg;
 

```

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [24 October 2021 14:43 UTC](https://discourse.nodered.org/t/question-about-js-function-return-msg/52661/2 "2021-10-24T14:43:54Z")

</div>

Is a matter of taste and it depends on your actual conditions. In your case it seems there could be "risk free" to return the msg as soon as a condition (EtapesCycle) and the rest in that block is fullfilled since then you eventually do not have to execute remaining code unnecessary

---

<div class="post-metadata">

**Author:** ![xavier98](https://avatars.discourse-cdn.com/v4/letter/x/f08c70/32.png) [@xavier98](https://discourse.nodered.org/u/xavier98)\
**Post date:** [24 October 2021 20:02 UTC](https://discourse.nodered.org/t/question-about-js-function-return-msg/52661/3 "2021-10-24T20:02:02Z")

</div>

Thank for your reply!

Now I have an error : "ReferenceError: message is not defined" after placed the return msg at the end of conditions

I don't know what is wrong 🤨

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [24 October 2021 20:44 UTC](https://discourse.nodered.org/t/question-about-js-function-return-msg/52661/4 "2021-10-24T20:44:24Z")

</div>

> [@xavier98](#):
>
> Now I have an error : "ReferenceError: message is not defined" after placed the return msg at the end of conditions

Are you sure you didnt write `return message` instead of `return msg`?

Show us your code.

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [24 October 2021 20:55 UTC](https://discourse.nodered.org/t/question-about-js-function-return-msg/52661/5 "2021-10-24T20:55:13Z")

</div>

I learnt programming a loong time ago and was always taught that there should only be 1 exit point but fashions change and as [krambriw](https://discourse.nodered.org/u/krambriw) says, these days it is a matter of choice. However, I would pick one method and stick with it as otherwise when you revisit your code in a few months time you will have to scratch your head to work put what is going on.

You have `EtapesCycle` set to 0 in the `//fin cycle if` but it is not used.

---

<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:** [25 October 2021 09:23 UTC](https://discourse.nodered.org/t/question-about-js-function-return-msg/52661/6 "2021-10-25T09:23:32Z")

</div>

> [@Buckskin](#):
>
> I learnt programming a loong time ago and was always taught that there should only be 1 exit point but fashions change

It isn't only a matter of fashion. There is a good reason for having only one return. Many times I have seen code inserted before the end of a function thinking that it will always be executed, whilst forgetting that there are returns earlier in the function.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [25 October 2021 11:07 UTC](https://discourse.nodered.org/t/question-about-js-function-return-msg/52661/7 "2021-10-25T11:07:12Z")

</div>

indeed - and sometimes it's good to check and bail out early rather than have to keep indenting loads of code blocks... But yes - returning in the middle of something usually does my head in.

---

<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:** [25 October 2021 13:11 UTC](https://discourse.nodered.org/t/question-about-js-function-return-msg/52661/8 "2021-10-25T13:11:21Z")

</div>

If I find the indentation getting too deep then I tend to think that the code is trying to tell me that I aught to be factoring some of it out into separate functions.

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [25 October 2021 18:36 UTC](https://discourse.nodered.org/t/question-about-js-function-return-msg/52661/9 "2021-10-25T18:36:17Z")

</div>

I totally agree but I have seen so much code since I started again (with Node-RED & Javascript) that I thought that I must just be old fashioned. I agree on the indenting also, that's what functions are for. 😃

---

<div class="post-metadata">

**Author:** ![xavier98](https://avatars.discourse-cdn.com/v4/letter/x/f08c70/32.png) [@xavier98](https://discourse.nodered.org/u/xavier98)\
**Post date:** [30 October 2021 14:28 UTC](https://discourse.nodered.org/t/question-about-js-function-return-msg/52661/10 "2021-10-30T14:28:28Z")

</div>

Hello 🙂

EtapesCyles is set to 0 for the standby step. So it can not enter in condition at the end of the cycle

---

<div class="post-metadata">

**Author:** ![xavier98](https://avatars.discourse-cdn.com/v4/letter/x/f08c70/32.png) [@xavier98](https://discourse.nodered.org/u/xavier98)\
**Post date:** [30 October 2021 14:35 UTC](https://discourse.nodered.org/t/question-about-js-function-return-msg/52661/11 "2021-10-30T14:35:24Z")

</div>

And I don't understand why I can't enter the 2nd condition when "xStartCycle" is TRUE and "poidsactuel" is 0.

Maybe I will share my project, it will be more simply for a help.

```auto
//Déclaration variables
var xStartCycle = false
const recette = []
var poidsactuel = flow.get("mesurepoids")
var EtapesCycle = 0

//Lancement cycle + tare balance + chargement recette 
if (msg.payload == true && xStartCycle == false){
    xStartCycle = true
    msg.tare = 1
    recette[1] = flow.get('rDosageLiquide1')
    recette[2] = flow.get('rDosageLiquide2')
    recette[3] = flow.get('rDosageLiquide3')
    recette[4] = flow.get('rDosageLiquide4')
    
} return msg

HERE:
//Vérification balance = 0g et lancement des étapes du cycle
if (xStartCycle == true && poidsactuel < 1 ){
    
    msg.tare = 0
   
     
    }return msg

    //Etapes cycle
    if(EtapesCycle == 1){
       if (poidsactuel < recette[EtapesCycle] && poidsactuel < 1){
           msg.result = 1
           
       } else if (poidsactuel >= recette[EtapesCycle]){
           msg.result = 0
           EtapesCycle = EtapesCycle +1
           msg.tare = 1
           
       } else if (recette[EtapesCycle] == 0){
           EtapesCycle = EtapesCycle +1
           
       }return msg
        
                        }
                        
   if(EtapesCycle == 2){
       if (poidsactuel < recette[EtapesCycle] && poidsactuel < 1){
           msg.result = 1
           
       } else if (poidsactuel >= recette[EtapesCycle]){
           msg.result = 0
           EtapesCycle = EtapesCycle +1
           msg.tare = 0
           
       } else if (recette[EtapesCycle] == 0){
           EtapesCycle = EtapesCycle +1
           
       }return msg
        
                        }
    
    if(EtapesCycle == 3){
       if (poidsactuel < recette[EtapesCycle] && poidsactuel < 1){
           msg.result = 1
           
       } else if (poidsactuel >= recette[EtapesCycle]){
           msg.result = 0
           EtapesCycle = EtapesCycle +1
           msg.tare = 1
           
       } else if (recette[EtapesCycle] == 0){
           EtapesCycle = EtapesCycle +1
           
       }return msg
        
                        }
                        
    if(EtapesCycle == 4){
       if (poidsactuel < recette[EtapesCycle] && poidsactuel < 1){
           msg.result = 1
           
       } else if (poidsactuel >= recette[EtapesCycle]){
           msg.result = 0
           xStartCycle = false
           msg.tare = 0
           
       } else if (recette[EtapesCycle] == 0){
           xStartCycle = false
       }return msg
        
                        }
                        
    //Fin cycle
    if(EtapesCycle == 4 && xStartCycle == false){
        EtapesCycle = 0
    }
    
   
 

```

---

<div class="post-metadata">

**Author:** ![xavier98](https://avatars.discourse-cdn.com/v4/letter/x/f08c70/32.png) [@xavier98](https://discourse.nodered.org/u/xavier98)\
**Post date:** [30 October 2021 14:36 UTC](https://discourse.nodered.org/t/question-about-js-function-return-msg/52661/12 "2021-10-30T14:36:39Z")

</div>

My project :

```auto
[
    {
        "id": "61e19b78ef64a3b5",
        "type": "tab",
        "label": "SMARTBAR",
        "disabled": false,
        "info": ""
    },
    {
        "id": "b787b38a3357045c",
        "type": "ui_slider",
        "z": "61e19b78ef64a3b5",
        "name": "",
        "label": "Dosage(ml) :",
        "tooltip": "",
        "group": "6b59a22e58b7fd02",
        "order": 3,
        "width": 0,
        "height": 0,
        "passthru": true,
        "outs": "end",
        "topic": "topic",
        "topicType": "msg",
        "min": 0,
        "max": "500",
        "step": "50",
        "className": "",
        "x": 470,
        "y": 140,
        "wires": [
            [
                "40009f74321bd978"
            ]
        ]
    },
    {
        "id": "0161651d99f59223",
        "type": "ui_text",
        "z": "61e19b78ef64a3b5",
        "group": "6b59a22e58b7fd02",
        "order": 1,
        "width": 0,
        "height": 0,
        "name": "",
        "label": "",
        "format": "{{msg.payload}}",
        "layout": "row-left",
        "className": "",
        "x": 450,
        "y": 100,
        "wires": []
    },
    {
        "id": "314adfebbf49060f",
        "type": "ui_text_input",
        "z": "61e19b78ef64a3b5",
        "name": "",
        "label": "",
        "tooltip": "",
        "group": "9d715f5db270a4ef",
        "order": 0,
        "width": 0,
        "height": 0,
        "passthru": true,
        "mode": "text",
        "delay": 300,
        "topic": "payload",
        "sendOnBlur": true,
        "topicType": "msg",
        "x": 160,
        "y": 100,
        "wires": [
            [
                "0161651d99f59223"
            ]
        ]
    },
    {
        "id": "345e47172f221bab",
        "type": "ui_text_input",
        "z": "61e19b78ef64a3b5",
        "name": "",
        "label": "",
        "tooltip": "",
        "group": "3052f4e65209445c",
        "order": 0,
        "width": 0,
        "height": 0,
        "passthru": true,
        "mode": "text",
        "delay": "300",
        "topic": "payload",
        "topicType": "msg",
        "x": 160,
        "y": 280,
        "wires": [
            [
                "37c6134abb86ec92"
            ]
        ]
    },
    {
        "id": "dd4fa6989bb62a19",
        "type": "ui_text_input",
        "z": "61e19b78ef64a3b5",
        "name": "",
        "label": "",
        "tooltip": "",
        "group": "dfac3fa00578e810",
        "order": 0,
        "width": 0,
        "height": 0,
        "passthru": true,
        "mode": "text",
        "delay": "300",
        "topic": "payload",
        "topicType": "msg",
        "x": 160,
        "y": 480,
        "wires": [
            [
                "6f5a18c3d422967e"
            ]
        ]
    },
    {
        "id": "b549c68ba4486d33",
        "type": "ui_text_input",
        "z": "61e19b78ef64a3b5",
        "name": "",
        "label": "",
        "tooltip": "",
        "group": "be945b5b01e73039",
        "order": 0,
        "width": 0,
        "height": 0,
        "passthru": true,
        "mode": "text",
        "delay": "300",
        "topic": "payload",
        "topicType": "msg",
        "x": 160,
        "y": 660,
        "wires": [
            [
                "953d7dac6d286fe8"
            ]
        ]
    },
    {
        "id": "880b5b52804e8ce9",
        "type": "ui_slider",
        "z": "61e19b78ef64a3b5",
        "name": "",
        "label": "Dosage(ml) :",
        "tooltip": "",
        "group": "3249f6ba14234d0b",
        "order": 3,
        "width": 0,
        "height": 0,
        "passthru": true,
        "outs": "end",
        "topic": "topic",
        "topicType": "msg",
        "min": 0,
        "max": "500",
        "step": "50",
        "className": "",
        "x": 470,
        "y": 320,
        "wires": [
            [
                "af2325ce4f368146"
            ]
        ]
    },
    {
        "id": "37c6134abb86ec92",
        "type": "ui_text",
        "z": "61e19b78ef64a3b5",
        "group": "3249f6ba14234d0b",
        "order": 1,
        "width": 0,
        "height": 0,
        "name": "",
        "label": "",
        "format": "{{msg.topic}}",
        "layout": "row-left",
        "className": "",
        "x": 450,
        "y": 280,
        "wires": []
    },
    {
        "id": "f14c353f79bae43c",
        "type": "ui_slider",
        "z": "61e19b78ef64a3b5",
        "name": "",
        "label": "Dosage(ml) :",
        "tooltip": "",
        "group": "2370014b9d0d6b45",
        "order": 3,
        "width": 0,
        "height": 0,
        "passthru": true,
        "outs": "end",
        "topic": "topic",
        "topicType": "msg",
        "min": 0,
        "max": "500",
        "step": "50",
        "className": "",
        "x": 470,
        "y": 520,
        "wires": [
            [
                "b0a77cf014cc4abb"
            ]
        ]
    },
    {
        "id": "6f5a18c3d422967e",
        "type": "ui_text",
        "z": "61e19b78ef64a3b5",
        "group": "2370014b9d0d6b45",
        "order": 1,
        "width": 0,
        "height": 0,
        "name": "",
        "label": "",
        "format": "{{msg.payload}}",
        "layout": "row-left",
        "className": "",
        "x": 450,
        "y": 480,
        "wires": []
    },
    {
        "id": "fde1baebbe32ba2f",
        "type": "comment",
        "z": "61e19b78ef64a3b5",
        "name": "Liquide 1",
        "info": "",
        "x": 160,
        "y": 60,
        "wires": []
    },
    {
        "id": "bce11f3491edf0c2",
        "type": "comment",
        "z": "61e19b78ef64a3b5",
        "name": "Liquide 4",
        "info": "",
        "x": 160,
        "y": 620,
        "wires": []
    },
    {
        "id": "e868200e7fd869f3",
        "type": "comment",
        "z": "61e19b78ef64a3b5",
        "name": "Liquide 3",
        "info": "",
        "x": 160,
        "y": 440,
        "wires": []
    },
    {
        "id": "aaa0a30d47f1ac6e",
        "type": "comment",
        "z": "61e19b78ef64a3b5",
        "name": "Liquide 2",
        "info": "",
        "x": 160,
        "y": 240,
        "wires": []
    },
    {
        "id": "20560ceaba0b9171",
        "type": "ui_slider",
        "z": "61e19b78ef64a3b5",
        "name": "",
        "label": "Dosage(ml) :",
        "tooltip": "",
        "group": "32773749ab4bc01c",
        "order": 3,
        "width": 0,
        "height": 0,
        "passthru": true,
        "outs": "end",
        "topic": "payload",
        "topicType": "msg",
        "min": 0,
        "max": "500",
        "step": "50",
        "className": "",
        "x": 470,
        "y": 700,
        "wires": [
            [
                "e350f373d75e9277"
            ]
        ]
    },
    {
        "id": "953d7dac6d286fe8",
        "type": "ui_text",
        "z": "61e19b78ef64a3b5",
        "group": "32773749ab4bc01c",
        "order": 1,
        "width": 0,
        "height": 0,
        "name": "",
        "label": "",
        "format": "{{msg.payload}}",
        "layout": "row-left",
        "className": "",
        "x": 450,
        "y": 660,
        "wires": []
    },
    {
        "id": "fdf8e06c611d1a02",
        "type": "comment",
        "z": "61e19b78ef64a3b5",
        "name": "Lancement de la commande",
        "info": "",
        "x": 220,
        "y": 840,
        "wires": []
    },
    {
        "id": "df51de8ac88f7a5f",
        "type": "ui_button",
        "z": "61e19b78ef64a3b5",
        "name": "",
        "group": "2304c94f9fc3e4e6",
        "order": 1,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "mi-local_bar",
        "payload": "true",
        "payloadType": "bool",
        "topic": "topic",
        "topicType": "msg",
        "x": 150,
        "y": 960,
        "wires": [
            [
                "1a3da4f9c9d1d315"
            ]
        ]
    },
    {
        "id": "af2325ce4f368146",
        "type": "function",
        "z": "61e19b78ef64a3b5",
        "name": "Set liquide 2",
        "func": "flow.set('rDosageLiquide2',msg.payload)\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 930,
        "y": 320,
        "wires": [
            []
        ]
    },
    {
        "id": "b0a77cf014cc4abb",
        "type": "function",
        "z": "61e19b78ef64a3b5",
        "name": "Set liquide 3",
        "func": "flow.set('rDosageLiquide3',msg.payload)\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 930,
        "y": 520,
        "wires": [
            []
        ]
    },
    {
        "id": "40009f74321bd978",
        "type": "function",
        "z": "61e19b78ef64a3b5",
        "name": "Set liquide 1",
        "func": "flow.set('rDosageLiquide1',msg.payload)\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 950,
        "y": 140,
        "wires": [
            []
        ]
    },
    {
        "id": "9acee912f403e6eb",
        "type": "hx711",
        "z": "61e19b78ef64a3b5",
        "name": "Cellule pesage",
        "hx_data": "29",
        "hx_sck": "31",
        "hx_scale": "1",
        "hx_offset": "0",
        "hx_avrg": "5",
        "x": 620,
        "y": 1020,
        "wires": [
            [
                "34ec2e0c0e5ab44c"
            ]
        ]
    },
    {
        "id": "7ed93540b59c7061",
        "type": "inject",
        "z": "61e19b78ef64a3b5",
        "name": "pesée",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "0.2",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payloadType": "date",
        "x": 400,
        "y": 1040,
        "wires": [
            [
                "9acee912f403e6eb"
            ]
        ]
    },
    {
        "id": "943cc83682e131eb",
        "type": "inject",
        "z": "61e19b78ef64a3b5",
        "name": "tare",
        "props": [
            {
                "p": "tare",
                "v": "",
                "vt": "date"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 410,
        "y": 1080,
        "wires": [
            [
                "9acee912f403e6eb"
            ]
        ]
    },
    {
        "id": "dd7f5c50e7f13f31",
        "type": "ui_gauge",
        "z": "61e19b78ef64a3b5",
        "name": "Affichage poids",
        "group": "6ff8ee57fd2a96bb",
        "order": 1,
        "width": 0,
        "height": 0,
        "gtype": "gage",
        "title": "",
        "label": "ml",
        "format": "{{msg.payload}}",
        "min": 0,
        "max": "1000",
        "colors": [
            "#00b500",
            "#e6e600",
            "#ca3838"
        ],
        "seg1": "",
        "seg2": "",
        "className": "",
        "x": 1100,
        "y": 1020,
        "wires": []
    },
    {
        "id": "34ec2e0c0e5ab44c",
        "type": "function",
        "z": "61e19b78ef64a3b5",
        "name": "Calcul masse",
        "func": "var buffer = msg.payload = Math.abs((msg.payload * 0.0005329949))\nmsg.payload = buffer.toFixed(1)\nflow.set(\"mesurepoids\", msg.payload)\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 880,
        "y": 1020,
        "wires": [
            [
                "dd7f5c50e7f13f31"
            ]
        ]
    },
    {
        "id": "e350f373d75e9277",
        "type": "function",
        "z": "61e19b78ef64a3b5",
        "name": "Set liquide 4",
        "func": "flow.set('rDosageLiquide4',msg.payload)\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 930,
        "y": 700,
        "wires": [
            []
        ]
    },
    {
        "id": "ab4c4eaf2aee5f30",
        "type": "rpi-gpio out",
        "z": "61e19b78ef64a3b5",
        "name": "",
        "pin": "40",
        "set": true,
        "level": "1",
        "freq": "",
        "out": "out",
        "x": 560,
        "y": 1360,
        "wires": []
    },
    {
        "id": "8dc7fc5918ec84a3",
        "type": "ui_switch",
        "z": "61e19b78ef64a3b5",
        "name": "",
        "label": "LED",
        "tooltip": "",
        "group": "caac32474324af48",
        "order": 0,
        "width": 0,
        "height": 0,
        "passthru": true,
        "decouple": "false",
        "topic": "topic",
        "topicType": "msg",
        "style": "",
        "onvalue": "false",
        "onvalueType": "bool",
        "onicon": "",
        "oncolor": "",
        "offvalue": "true",
        "offvalueType": "bool",
        "officon": "",
        "offcolor": "",
        "animate": false,
        "x": 310,
        "y": 1360,
        "wires": [
            [
                "ab4c4eaf2aee5f30"
            ]
        ]
    },
    {
        "id": "1a3da4f9c9d1d315",
        "type": "function",
        "z": "61e19b78ef64a3b5",
        "name": "GestionCycle",
        "func": "//Déclaration variables\nvar xStartCycle = false\nconst recette = []\nvar poidsactuel = flow.get(\"mesurepoids\")\nvar EtapesCycle = 0\n\n//Lancement cycle + tare balance + chargement recette \nif (msg.payload == true && xStartCycle == false){\n xStartCycle = true\n msg.tare = 1\n recette[1] = flow.get('rDosageLiquide1')\n recette[2] = flow.get('rDosageLiquide2')\n recette[3] = flow.get('rDosageLiquide3')\n recette[4] = flow.get('rDosageLiquide4')\n \n} return msg\n\n//Vérification balance = 0g et lancement des étapes du cycle\nif (xStartCycle == true && poidsactuel < 1 ){\n \n msg.tare = 0\n \n \n }return msg\n\n //Etapes cycle\n if(EtapesCycle == 1){\n if (poidsactuel < recette[EtapesCycle] && poidsactuel < 1){\n msg.result = 1\n \n } else if (poidsactuel >= recette[EtapesCycle]){\n msg.result = 0\n EtapesCycle = EtapesCycle +1\n msg.tare = 1\n \n } else if (recette[EtapesCycle] == 0){\n EtapesCycle = EtapesCycle +1\n \n }return msg\n \n }\n \n if(EtapesCycle == 2){\n if (poidsactuel < recette[EtapesCycle] && poidsactuel < 1){\n msg.result = 1\n \n } else if (poidsactuel >= recette[EtapesCycle]){\n msg.result = 0\n EtapesCycle = EtapesCycle +1\n msg.tare = 0\n \n } else if (recette[EtapesCycle] == 0){\n EtapesCycle = EtapesCycle +1\n \n }return msg\n \n }\n \n if(EtapesCycle == 3){\n if (poidsactuel < recette[EtapesCycle] && poidsactuel < 1){\n msg.result = 1\n \n } else if (poidsactuel >= recette[EtapesCycle]){\n msg.result = 0\n EtapesCycle = EtapesCycle +1\n msg.tare = 1\n \n } else if (recette[EtapesCycle] == 0){\n EtapesCycle = EtapesCycle +1\n \n }return msg\n \n }\n \n if(EtapesCycle == 4){\n if (poidsactuel < recette[EtapesCycle] && poidsactuel < 1){\n msg.result = 1\n \n } else if (poidsactuel >= recette[EtapesCycle]){\n msg.result = 0\n xStartCycle = false\n msg.tare = 0\n \n } else if (recette[EtapesCycle] == 0){\n xStartCycle = false\n }return msg\n \n }\n \n //Fin cycle\n if(EtapesCycle == 4 && xStartCycle == false){\n EtapesCycle = 0\n }\n \n \n ",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 380,
        "y": 960,
        "wires": [
            [
                "9acee912f403e6eb",
                "e6d0e4ad85025d04",
                "27fe1f4b372b9991",
                "db7ebfc3405a8f63"
            ]
        ]
    },
    {
        "id": "e6d0e4ad85025d04",
        "type": "debug",
        "z": "61e19b78ef64a3b5",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "tare",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 610,
        "y": 900,
        "wires": []
    },
    {
        "id": "27fe1f4b372b9991",
        "type": "debug",
        "z": "61e19b78ef64a3b5",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 620,
        "y": 840,
        "wires": []
    },
    {
        "id": "16b16599fa1a1410",
        "type": "ui_button",
        "z": "61e19b78ef64a3b5",
        "name": "Tare",
        "group": "6ff8ee57fd2a96bb",
        "order": 2,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "Tare",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "",
        "payloadType": "date",
        "topic": "topic",
        "topicType": "msg",
        "x": 150,
        "y": 1140,
        "wires": [
            [
                "e6984ec64b9ac543"
            ]
        ]
    },
    {
        "id": "e6984ec64b9ac543",
        "type": "function",
        "z": "61e19b78ef64a3b5",
        "name": "",
        "func": "msg.tare = 1\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 390,
        "y": 1140,
        "wires": [
            [
                "9acee912f403e6eb"
            ]
        ]
    },
    {
        "id": "695e96b922da21e0",
        "type": "comment",
        "z": "61e19b78ef64a3b5",
        "name": "Gestion LED",
        "info": "",
        "x": 150,
        "y": 1240,
        "wires": []
    },
    {
        "id": "db7ebfc3405a8f63",
        "type": "debug",
        "z": "61e19b78ef64a3b5",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "result",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 610,
        "y": 960,
        "wires": []
    },
    {
        "id": "6b59a22e58b7fd02",
        "type": "ui_group",
        "name": "Liquide1",
        "tab": "d260e8fa5283fae3",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "9d715f5db270a4ef",
        "type": "ui_group",
        "name": "Liquide 1",
        "tab": "d6360769c3bcc5e8",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false
    },
    {
        "id": "3052f4e65209445c",
        "type": "ui_group",
        "name": "Liquide 2",
        "tab": "d6360769c3bcc5e8",
        "order": 2,
        "disp": true,
        "width": "6",
        "collapse": false
    },
    {
        "id": "dfac3fa00578e810",
        "type": "ui_group",
        "name": "Liquide 3",
        "tab": "d6360769c3bcc5e8",
        "order": 3,
        "disp": true,
        "width": "6",
        "collapse": false
    },
    {
        "id": "be945b5b01e73039",
        "type": "ui_group",
        "name": "Liquide 4",
        "tab": "d6360769c3bcc5e8",
        "order": 4,
        "disp": true,
        "width": "6",
        "collapse": false
    },
    {
        "id": "3249f6ba14234d0b",
        "type": "ui_group",
        "name": "Liquide2",
        "tab": "d260e8fa5283fae3",
        "order": 2,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "2370014b9d0d6b45",
        "type": "ui_group",
        "name": "Liquide3",
        "tab": "d260e8fa5283fae3",
        "order": 3,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "32773749ab4bc01c",
        "type": "ui_group",
        "name": "Liquide4",
        "tab": "d260e8fa5283fae3",
        "order": 4,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "2304c94f9fc3e4e6",
        "type": "ui_group",
        "name": "Commande",
        "tab": "d260e8fa5283fae3",
        "order": 5,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "6ff8ee57fd2a96bb",
        "type": "ui_group",
        "name": "Balance",
        "tab": "d6360769c3bcc5e8",
        "order": 6,
        "disp": true,
        "width": "6",
        "collapse": false
    },
    {
        "id": "caac32474324af48",
        "type": "ui_group",
        "name": "Lumière",
        "tab": "d6360769c3bcc5e8",
        "order": 5,
        "disp": false,
        "width": "6",
        "collapse": false
    },
    {
        "id": "d260e8fa5283fae3",
        "type": "ui_tab",
        "name": "Commande",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    },
    {
        "id": "d6360769c3bcc5e8",
        "type": "ui_tab",
        "name": "Param.",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    }
]

```

---

<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:** [30 October 2021 14:42 UTC](https://discourse.nodered.org/t/question-about-js-function-return-msg/52661/13 "2021-10-30T14:42:07Z")

</div>

> [@xavier98](#):
>
> ```auto
> } return msg
> 
> HERE:
> //Vérification balance = 0g et lancement des étapes du cycle
> if (xStartCycle == true && poidsactuel < 1 ){
> 
> ```

You have a `return` statement outside the `if`. That causes the function to return and no code past that point is ever executed.

**[Edit]** and I notice you have done the same thing several times further down.

---

<div class="post-metadata">

**Author:** ![xavier98](https://avatars.discourse-cdn.com/v4/letter/x/f08c70/32.png) [@xavier98](https://discourse.nodered.org/u/xavier98)\
**Post date:** [30 October 2021 14:48 UTC](https://discourse.nodered.org/t/question-about-js-function-return-msg/52661/14 "2021-10-30T14:48:51Z")

</div>

Ok thanks, so as we were talking about before, you have to put only a return at the end to avoid that?

Later it will be optimize with a loop 😉

---

<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:** [30 October 2021 14:51 UTC](https://discourse.nodered.org/t/question-about-js-function-return-msg/52661/15 "2021-10-30T14:51:39Z")

</div>

What do you want to do after the if block has either been executed or skipped? If you just want to drop down to the next line then why have you put a return statement there?

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [30 October 2021 14:53 UTC](https://discourse.nodered.org/t/question-about-js-function-return-msg/52661/16 "2021-10-30T14:53:37Z")

</div>

This more about javascript, not so much NR  
I suggest you begin reading up on javascript syntax to start, it will help you very much on your journey

> **[JavaScript Tutorial](https://www.w3schools.com/js/)**
>
> W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

---

<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:** [29 December 2021 14:54 UTC](https://discourse.nodered.org/t/question-about-js-function-return-msg/52661/17 "2021-12-29T14:54:25Z")

</div>

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