# Function Not Returning Value

**URL:** <https://discourse.nodered.org/t/function-not-returning-value/49854>\
**Category:** General\
**Created:** [17 August 2021 18:48 UTC](https://discourse.nodered.org/t/function-not-returning-value/49854 "2021-08-17T18:48:58Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChZeman](https://avatars.discourse-cdn.com/v4/letter/c/76d3ee/32.png) [@ChZeman](https://discourse.nodered.org/u/ChZeman)\
**Post date:** [17 August 2021 18:48 UTC](https://discourse.nodered.org/t/function-not-returning-value/49854/1 "2021-08-17T18:48:58Z")

</div>

I have a function in a node that searches for and returns the line of a text file. It does what it should, but the output is only assigned to the variable inside the function. Additionally, the debug output seems to indicate that the line after I call the function is being called first. I've tried all sorts of things the past several hours but can't put my finger on what the problem is.

Thank you,  
Chris

```auto
function Get_Message(File, msgNumber){
    // Check if the file exists.
    if (parseInt(msgNumber) != 0) {
        fs.access(File, fs.F_OK, (err) => {
            if (err) {
                node.warn(err);
            }

            // File exists.
            var line = msgNumber - 1;
        
            var myInterface = readline.createInterface({
                input: fs.createReadStream(File)
            });

            var lineno = 0;

            myInterface.on('line', function (line) {
                lineno++;
            
                if (lineno == msgNumber){
                    var Message = line;
                    node.warn(Message)
                }
            });
        })
    }
    
    return Message;
}

```

```auto
if (parseInt(Payload["Error"]) != 0){
    File = "/home/pi/Documents/Machines/" + Machine + "/" + Machine + " Errors.txt";
    var Message = Get_Message(File, Payload["Error"])||null;
    node.warn("1: " + Message);
} else {
    File = "/home/pi/Documents/Machines/" + Machine + "/" + Machine + " Status.txt";
    var Message = Get_Message(File, Payload["Status"])||null;
    node.warn("2: " + Message);
}

```

![MWSnap 2021-08-17, 13_47_15](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/d/dd14e1579b55ebd1a29c5adaf8dcb2024a75a59e.png)

---

<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:** [17 August 2021 18:59 UTC](https://discourse.nodered.org/t/function-not-returning-value/49854/2 "2021-08-17T18:59:46Z")

</div>

fs.access is an async function so return will not work. Use node.send instead.

Ps, why are you using fs in a function instead of the built-in/for purpose "file in" node?

---

<div class="post-metadata">

**Author:** ![kevinGodell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kevingodell/32/27040_2.png) [@kevinGodell](https://discourse.nodered.org/u/kevinGodell)\
**Post date:** [17 August 2021 18:59 UTC](https://discourse.nodered.org/t/function-not-returning-value/49854/3 "2021-08-17T18:59:59Z")

</div>

fs.access seems to be asynchronous. You will have to wait for it to be finished then use node.send(msg) vs just returning the message.

edit  
@Steve-Mcl beat me to the answer

---

<div class="post-metadata">

**Author:** ![ChZeman](https://avatars.discourse-cdn.com/v4/letter/c/76d3ee/32.png) [@ChZeman](https://discourse.nodered.org/u/ChZeman)\
**Post date:** [17 August 2021 19:16 UTC](https://discourse.nodered.org/t/function-not-returning-value/49854/4 "2021-08-17T19:16:16Z")

</div>

I'm deciding which file just before I call the function. It seems far more efficient to read the file at the time I need it and continue processing from where I'm at, rather than daisy-chaining even more nodes.

Thank you, guys. I'll take a look at node.send.

---

<div class="post-metadata">

**Author:** ![ChZeman](https://avatars.discourse-cdn.com/v4/letter/c/76d3ee/32.png) [@ChZeman](https://discourse.nodered.org/u/ChZeman)\
**Post date:** [17 August 2021 19:42 UTC](https://discourse.nodered.org/t/function-not-returning-value/49854/5 "2021-08-17T19:42:43Z")

</div>

It doesn't look like that's going to work, either. Thanks anyways. I'll keep hunting for a solution.

---

<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:** [17 August 2021 19:48 UTC](https://discourse.nodered.org/t/function-not-returning-value/49854/6 "2021-08-17T19:48:16Z")

</div>

It will. Or... You could use a function to calculate a filename, set `msg.filename`, pass the msg to a file in node & do it the graphical/node-red way

I know you said it's not efficient but unless you are doing many thousands of operations, the difference will be almost nothing.

---

<div class="post-metadata">

**Author:** ![ChZeman](https://avatars.discourse-cdn.com/v4/letter/c/76d3ee/32.png) [@ChZeman](https://discourse.nodered.org/u/ChZeman)\
**Post date:** [17 August 2021 20:20 UTC](https://discourse.nodered.org/t/function-not-returning-value/49854/7 "2021-08-17T20:20:54Z")

</div>

Fair enough. I did what you suggested and am reading the file. I now have a rather large string I need to parse. I was looking to grab a specific line from the file.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [17 August 2021 20:40 UTC](https://discourse.nodered.org/t/function-not-returning-value/49854/8 "2021-08-17T20:40:08Z")

</div>

you can set your **File-in** node to  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/f/3f7f38438237316a40eb01e5ce93b239770324fd.png)

and process each line separately  
or if you want to join all lines to an array then  
after your **File-in** use a **Join node**

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

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [17 August 2021 20:40 UTC](https://discourse.nodered.org/t/function-not-returning-value/49854/9 "2021-08-17T20:40:43Z")

</div>

You could use an exec node and grep to find the line

---

<div class="post-metadata">

**Author:** ![ChZeman](https://avatars.discourse-cdn.com/v4/letter/c/76d3ee/32.png) [@ChZeman](https://discourse.nodered.org/u/ChZeman)\
**Post date:** [17 August 2021 21:04 UTC](https://discourse.nodered.org/t/function-not-returning-value/49854/10 "2021-08-17T21:04:18Z")

</div>

Thank you. I used a function to set my filename and combine the results into an array. I had to create a global variable in the same function to get the original msg.payload to the end function. All set. Thanks, guys!

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [17 August 2021 21:12 UTC](https://discourse.nodered.org/t/function-not-returning-value/49854/11 "2021-08-17T21:12:55Z")

</div>

> [@ChZeman](#):
>
> I had to create a global variable in the same function to get the original msg.payload to the end function

if you needed to pass the original payload further down the flow line ..  
since you used a function .. you could simply transfer the original payload to another msg property to protect it .. because yes, the payload would have been replaced by the file-in node's output.

`msg.originalPayload = RED.util.cloneMessage(msg.payload)`  
**[EDIT]** we clone the msg with NR util to avoid by reference

Set file-in to ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/0/c00a58cf30fbbe1dea3a29fd5350d2e7a680ba33.png)

Context is useful also.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [17 August 2021 22:47 UTC](https://discourse.nodered.org/t/function-not-returning-value/49854/12 "2021-08-17T22:47:17Z")

</div>

> [@UnborN](#):
>
> you could simply transfer the original payload to another msg property

Doesn't it need cloning first?

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [17 August 2021 23:00 UTC](https://discourse.nodered.org/t/function-not-returning-value/49854/13 "2021-08-17T23:00:23Z")

</div>

Very good point .. i always forget about that 😉  
(I'll edit the above post)

---

<div class="post-metadata">

**Author:** ![ChZeman](https://avatars.discourse-cdn.com/v4/letter/c/76d3ee/32.png) [@ChZeman](https://discourse.nodered.org/u/ChZeman)\
**Post date:** [17 August 2021 23:03 UTC](https://discourse.nodered.org/t/function-not-returning-value/49854/14 "2021-08-17T23:03:17Z")

</div>

I ended up doing most of the processing in the first node and assigning everything to a new object that I made global. All worked out well. Thank you! I appreciate it!

---

<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:** [31 August 2021 23:03 UTC](https://discourse.nodered.org/t/function-not-returning-value/49854/15 "2021-08-31T23:03:18Z")

</div>

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