# Returning asyc function (mysql select query)

**URL:** https://discourse.nodered.org/t/returning-asyc-function-mysql-select-query/47381
**Category:** General
**Created:** [19 June 2021 23:13 UTC](https://discourse.nodered.org/t/returning-asyc-function-mysql-select-query/47381 "2021-06-19T23:13:18Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Ricardo-Miguel-Calde](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@Ricardo-Miguel-Calde](https://discourse.nodered.org/u/Ricardo-Miguel-Calde)
#### Post date: [19 June 2021 23:13 UTC](https://discourse.nodered.org/t/returning-asyc-function-mysql-select-query/47381/1 "2021-06-19T23:13:18Z")

</div>

Hello and TIA for anyone that could lead me to a solution..

I'm trying to make a SELECT query to a mysql database using mysql2 but the result when calling the function is always a promisse.  
I do manage to get data from the database but only inside the function...

Any help is appreciated. Thanks!

I've set node-red settinhs.js as:

```auto

    functionGlobalContext: {
        os:require('os'),
        fs:require('fs'),
        mysql2:require('mysql2'),
        // jfive:require("johnny-five"),
        // j5board:require("johnny-five").Board({repl:false})
    },

    // Allow the Function node to load additional npm modules
    functionExternalModules: true,

```

I do have the console showing data:  
 ![mysql async](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/4/a4eab5212c483af3e88ae4e219efd9e6e42b83d7.png)

Flow:

```auto
[{"id":"e31e66e2.5af808","type":"function","z":"c4f601fe.4aba7","name":"getDevices","func":"\ncontext.global.getDevices = async function getData(){\n \n //Function checkin\n node.warn(\"DEBUG 1\")\n \n const mysql = global.get(\"mysql2\")\n \n //create the pool\n const connection = await mysql.createConnection({host:'--YOURSERVER--', user: '--YOURUSERNAME--', password:'--YOURPASS--', database: 'mika'});\n const [rows, fields] = await connection.promise().execute('SELECT 1');\n \n\n node.warn(\"DEBUG 2\" + JSON.stringify(rows))\n //Data is printed in console log.\n \n return rows\n \n}\n\n//Calls function and display the results\nnode.warn(\"RESULT is \" + context.global.getDevices());\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"mysql","module":"mysql2"}],"x":1030,"y":240,"wires":[[]]},{"id":"77d1c752.3dabd8","type":"inject","z":"c4f601fe.4aba7","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"mysql","payload":"","payloadType":"date","x":1040,"y":160,"wires":[["e31e66e2.5af808"]]}]

```

---

<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: [19 June 2021 23:23 UTC](https://discourse.nodered.org/t/returning-asyc-function-mysql-select-query/47381/2 "2021-06-19T23:23:28Z")

</div>

Hi @Ricardo-Miguel-Calde out of curiosity why are you coding mySQL in a function instead of using the mySQL node-red node?

As for your question, you should call .then() and .catch() on the promise. Inside the `then` handler, you would call node.send() to pass the data out of the function node.

---

<div class="post-metadata">

### Author: ![Ricardo-Miguel-Calde](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@Ricardo-Miguel-Calde](https://discourse.nodered.org/u/Ricardo-Miguel-Calde)
#### Post date: [19 June 2021 23:38 UTC](https://discourse.nodered.org/t/returning-asyc-function-mysql-select-query/47381/3 "2021-06-19T23:38:51Z")

</div>

Reusing mostly. I need to get data along the flow. This function will simplify the entire project.

I did try that also and I get the same result.

```auto
    //create the pool
    const connection = mysql.createConnection({host:'--HOST--', user: '--YOURUSER--', password:'--YOURPASSWORD--', database: '--DBNAME--'});
    const [rows, fields] = await connection.promise().execute('SELECT 1').then((rows)=>{
        return rows
    });

```

I do think it's related to the call itself.

To use this library I should require("mysql2/promise") but when I do the "translated" statement for nodered as global.get("mysql2/promise") the result is the same except in the console log I can't see "Debug 2" printed.

Nothing is printed in the node-red log also...

[https://www.npmjs.com/package/mysql2#using-promise-wrapper](https://www.npmjs.com/package/mysql2#using-promise-wrapper)

---

<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: [19 June 2021 23:51 UTC](https://discourse.nodered.org/t/returning-asyc-function-mysql-select-query/47381/4 "2021-06-19T23:51:06Z")

</div>

> [@Ricardo-Miguel-Calde](#):
>
> This function will simplify the entire project.

You would probably already be up and running already if you install/use node-red-node-mysql. It handles connections, pools, reconnections, safely encrypts connection details, handles errors by directing them to the appropriate logs, etc etc etc etc.

You do realise a function node is fully executed every time it is called (meaning you will be continuously connecting and destroying connections unless you use context memory or some other mechanism to maintain connections?

> [@Ricardo-Miguel-Calde](#):
>
> ```auto
> const [rows, fields] = await connection.promise().execute('SELECT 1').then((rows)=>{
> return rows
> });
> 
> ```

As the code is asynchronous, you cannot return. You must call node.send as I said earlier. E.g...

```auto
node.send({payload: rows});

```

---

<div class="post-metadata">

### Author: ![Ricardo-Miguel-Calde](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@Ricardo-Miguel-Calde](https://discourse.nodered.org/u/Ricardo-Miguel-Calde)
#### Post date: [20 June 2021 00:02 UTC](https://discourse.nodered.org/t/returning-asyc-function-mysql-select-query/47381/5 "2021-06-20T00:02:14Z")

</div>

I see...

I was really looking for a solution to **return** data from the function... Can't see how **node.send** will help me (unless I set a global and wait for the data change)...

Thanks anyway! Cheers!

---

<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: [20 June 2021 00:04 UTC](https://discourse.nodered.org/t/returning-asyc-function-mysql-select-query/47381/6 "2021-06-20T00:04:42Z")

</div>

> [@Ricardo-Miguel-Calde](#):
>
> Can't see how **node.send** will help me

Node send sends it out of the function. I.e. allows you to pass to the next node (or wherever you wish for the data to go)

If you want to process the data inside the function, do it inside the `then` handler.

I'm not quite understanding your issue (note I haven't loaded your flow so I am working from what you write)

---

<div class="post-metadata">

### Author: ![Ricardo-Miguel-Calde](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@Ricardo-Miguel-Calde](https://discourse.nodered.org/u/Ricardo-Miguel-Calde)
#### Post date: [20 June 2021 00:10 UTC](https://discourse.nodered.org/t/returning-asyc-function-mysql-select-query/47381/7 "2021-06-20T00:10:43Z")

</div>

Just need this function to return the data so it can be used inside other functions... The objective is not to pass it to other nodes at all..

---

<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: [20 June 2021 00:14 UTC](https://discourse.nodered.org/t/returning-asyc-function-mysql-select-query/47381/8 "2021-06-20T00:14:35Z")

</div>

Ah. Well. That it pretty much the opposite of what node-red is about tbh.

What is your aversion to passing the data? I'm guessing some design direction?

If you need to, you can store the data in flow or global context for later use - which you can access in another function using flow.get or global.get.

---

<div class="post-metadata">

### Author: ![Ricardo-Miguel-Calde](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@Ricardo-Miguel-Calde](https://discourse.nodered.org/u/Ricardo-Miguel-Calde)
#### Post date: [20 June 2021 01:08 UTC](https://discourse.nodered.org/t/returning-asyc-function-mysql-select-query/47381/10 "2021-06-20T01:08:38Z")

</div>

Solution for my own case is:

[https://flows.nodered.org/flow/1d626aac6273d16d5eefd74aafdaee59](https://flows.nodered.org/flow/1d626aac6273d16d5eefd74aafdaee59)

---

<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: [4 July 2021 01:09 UTC](https://discourse.nodered.org/t/returning-asyc-function-mysql-select-query/47381/11 "2021-07-04T01:09:13Z")

</div>

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