# Pass S7 variable to Mysql query

**URL:** <https://discourse.nodered.org/t/pass-s7-variable-to-mysql-query/41484>\
**Category:** General\
**Created:** [23 February 2021 13:29 UTC](https://discourse.nodered.org/t/pass-s7-variable-to-mysql-query/41484 "2021-02-23T13:29:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eimerich](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@Eimerich](https://discourse.nodered.org/u/Eimerich)\
**Post date:** [23 February 2021 13:29 UTC](https://discourse.nodered.org/t/pass-s7-variable-to-mysql-query/41484/1 "2021-02-23T13:29:21Z")

</div>

Hi there, I've go through different posts here, but wasn't able to find a proper solution.  
It's my first go with Node-Red and my objective is the following:  
I have sensors variables from S7 PLC from which I have to get value to populate a Mysql db.  
I've mounted the Mysql db node with no problems and start testing the first "Insert into" queries.

Actually I'm stuck in this situation:  
I have a PLC S7 In node that is creating a msg.payload named "metri" that is a number (as I can see from the debug console).  
I've created a function node with the following:

```auto
var metri=msg.payload.metri;
msg.topic = "INSERT INTO test_iot (metri) VALUE ('" + metri + "')";
return msg;

```

From the debug console, I see this message:  
INSERT INTO test\_iot (metri) VALUE ('undefined').

What I'm missing?

Thanks in advance for your help  
Michele

---

<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:** [23 February 2021 13:50 UTC](https://discourse.nodered.org/t/pass-s7-variable-to-mysql-query/41484/2 "2021-02-23T13:50:00Z")

</div>

Welcome to the forum @Eimerich.

That error probably means that msg.payload.metri does not exist. Connect a debug node showing what is going into the function node and seewhat it shows.  
First though, if you are a beginner with node-red then I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.  
Also read through the node-red doc Working with Messages which should also help, and in particular will show you how to get the path to an item in the debug pane (the path is how to address it, msg.payload.metri for example).

Then if you are still stuck come back again and show us what is going into the function.

---

<div class="post-metadata">

**Author:** ![Eimerich](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@Eimerich](https://discourse.nodered.org/u/Eimerich)\
**Post date:** [23 February 2021 14:09 UTC](https://discourse.nodered.org/t/pass-s7-variable-to-mysql-query/41484/3 "2021-02-23T14:09:46Z")

</div>

Hi Colin,  
thanks a lot for your reply.  
I'll surely have a look into the suggested Essentials.  
In the meantime this is the debug from the S7 Node

```auto
23/2/2021, 15:04:51node: 3f5809af.a2d146
metri : msg.payload : number
7654

```

I'm passing the wrong value to the function?  
In the meantime I'm going to "EssentialsMe"...

Thanks!  
Michele

---

<div class="post-metadata">

**Author:** ![Eimerich](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@Eimerich](https://discourse.nodered.org/u/Eimerich)\
**Post date:** [23 February 2021 14:59 UTC](https://discourse.nodered.org/t/pass-s7-variable-to-mysql-query/41484/4 "2021-02-23T14:59:03Z")

</div>

Hi found out the error @Colin !  
You were right! My msg.payload.metri wasn't there.  
I wrongly assumed that the "variable" name you give to a node was to be consider as its "name", when it wasn't.  
Now I'm properly writing on the MySQL db the meters.  
Time to a new adventure!  
Thanks!  
Michele

---

<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:** [9 March 2021 14:59 UTC](https://discourse.nodered.org/t/pass-s7-variable-to-mysql-query/41484/5 "2021-03-09T14:59:58Z")

</div>

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