# Query to write data to mySQL database(error)

**URL:** <https://discourse.nodered.org/t/query-to-write-data-to-mysql-database-error/65128>\
**Category:** General\
**Tags:** database, function-node\
**Created:** [15 July 2022 08:20 UTC](https://discourse.nodered.org/t/query-to-write-data-to-mysql-database-error/65128 "2022-07-15T08:20:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![JackJackTan414](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@JackJackTan414](https://discourse.nodered.org/u/JackJackTan414)\
**Post date:** [15 July 2022 08:20 UTC](https://discourse.nodered.org/t/query-to-write-data-to-mysql-database-error/65128/1 "2022-07-15T08:20:14Z")

</div>

Hi. kindly help me cross check what seems to be the error here. I want to pass an input string value to "msg.text" variable and i want it to write on mySQL database. my "msg.payload" works fine, the issue lies only on the "msg.text".

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/5/258a815b68d6caa5820933445f3eadf3776cb5d1.png)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/b/ab7e5d663e7d6cdc9f845bf019691657e55b96e5.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:** [15 July 2022 08:29 UTC](https://discourse.nodered.org/t/query-to-write-data-to-mysql-database-error/65128/2 "2022-07-15T08:29:36Z")

</div>

> [@JackJackTan414](#):
>
> the issue lies only on the "msg.text".

Does msg.text actually have a value?

Use a debug node (set to show complete msg) BEFORE the function node.  
OR  
add `node.warn(msg.text)` in your function BEFORE setting msg.topic

Lastly - it is strongly recommended you use prepared queries / parameters instead of generating a SQL Query string (it is unsafe / prone to SQL injection) - read the node-red-node-mysql help to see how to use parameters.

---

<div class="post-metadata">

**Author:** ![JackJackTan414](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@JackJackTan414](https://discourse.nodered.org/u/JackJackTan414)\
**Post date:** [15 July 2022 08:48 UTC](https://discourse.nodered.org/t/query-to-write-data-to-mysql-database-error/65128/3 "2022-07-15T08:48:41Z")

</div>

this "msg.text" takes in the string character from whatever i input on the dashboard. I tested on a separate flow and this works fine. but this problem arises when i try to included it in the query.

I tried changing the debug node to display "all msg object" and i manage to get the msg.text data as "a" string value.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/7/d7367b2b609f1aff915341ecba266f2088846fe8.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:** [15 July 2022 08:52 UTC](https://discourse.nodered.org/t/query-to-write-data-to-mysql-database-error/65128/4 "2022-07-15T08:52:51Z")

</div>

This time yes, but your first error was different - it showed `undefined` so I can pretty much guarantee `msg.text` was NOT present.

this time you have a different error and it is coming from a different node (i can see this one is named `sends_data_to_SQL` where as the previous error came from an unnamed node).

For reference: `toFixed` only works on numbers (you must be calling `toFixed` on a string or something else inside `sends_data_to_SQL`). Use the same tips I showed you previously to determine what that is.

---

<div class="post-metadata">

**Author:** ![JackJackTan414](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@JackJackTan414](https://discourse.nodered.org/u/JackJackTan414)\
**Post date:** [15 July 2022 09:02 UTC](https://discourse.nodered.org/t/query-to-write-data-to-mysql-database-error/65128/5 "2022-07-15T09:02:55Z")

</div>

i only called ToFixed() for msg.payload which is a number. and it works as shown.

sends\_data\_toSQL is basically a function node which contains the query as shown in the image above.

---

<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:** [15 July 2022 09:05 UTC](https://discourse.nodered.org/t/query-to-write-data-to-mysql-database-error/65128/6 "2022-07-15T09:05:57Z")

</div>

> [@JackJackTan414](#):
>
> i only called ToFixed() for msg.payload which is a number. and it works as shown.

ummmm...

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

I say what I see!

* * *

I'm sorry, but you are not clear and not sharing enough related info (I dont even know the arrangement of your flows) so I cant really help without context!

---

<div class="post-metadata">

**Author:** ![JackJackTan414](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@JackJackTan414](https://discourse.nodered.org/u/JackJackTan414)\
**Post date:** [15 July 2022 13:43 UTC](https://discourse.nodered.org/t/query-to-write-data-to-mysql-database-error/65128/7 "2022-07-15T13:43:01Z")

</div>

hi. So sorry, i will try to attach my whole flow here next week. office is closed on weekends.

---

<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:** [13 September 2022 13:43 UTC](https://discourse.nodered.org/t/query-to-write-data-to-mysql-database-error/65128/8 "2022-09-13T13:43:07Z")

</div>

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