# What's wrong with msg.topic : the query is not defined as a string?

**URL:** <https://discourse.nodered.org/t/whats-wrong-with-msg-topic-the-query-is-not-defined-as-a-string/39756>\
**Category:** General\
**Created:** [26 January 2021 07:17 UTC](https://discourse.nodered.org/t/whats-wrong-with-msg-topic-the-query-is-not-defined-as-a-string/39756 "2021-01-26T07:17:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arthur123124](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Arthur123124](https://discourse.nodered.org/u/Arthur123124)\
**Post date:** [26 January 2021 07:17 UTC](https://discourse.nodered.org/t/whats-wrong-with-msg-topic-the-query-is-not-defined-as-a-string/39756/1 "2021-01-26T07:17:09Z")

</div>

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

i want to collect data and then send it to database, but it said msg.topic : the query is not defined as a string". i don't know how to fix it.

---

<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:** [26 January 2021 07:26 UTC](https://discourse.nodered.org/t/whats-wrong-with-msg-topic-the-query-is-not-defined-as-a-string/39756/2 "2021-01-26T07:26:34Z")

</div>

Difficult to tell from just a picture.

Either share your flow or show where you set topic.

Ps, try adding `node.warn(typeof msg.topic)` in the function AFTER you have generated your SQL statement.

---

<div class="post-metadata">

**Author:** ![Arthur123124](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Arthur123124](https://discourse.nodered.org/u/Arthur123124)\
**Post date:** [26 January 2021 07:28 UTC](https://discourse.nodered.org/t/whats-wrong-with-msg-topic-the-query-is-not-defined-as-a-string/39756/3 "2021-01-26T07:28:24Z")

</div>

```auto
[{"id":"faa8e95.a885c18","type":"tab","label":"Database1","disabled":false,"info":"定時send data to database\ndata有變化會send database"},{"id":"b406cf79.f68bd","type":"delay","z":"faa8e95.a885c18","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"15","rateUnits":"minute","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":500,"y":140,"wires":[["444c3494.0790ec","9e9af451.5699d8"]]},{"id":"6b1cc7cb.f7c658","type":"rbe","z":"faa8e95.a885c18","name":"block","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":770,"y":220,"wires":[["635751e4.1dd69"]]},{"id":"9e9af451.5699d8","type":"debug","z":"faa8e95.a885c18","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":750,"y":140,"wires":[]},{"id":"553bdf96.1262","type":"function","z":"faa8e95.a885c18","name":"小數點後兩個位","func":"msg.payload = (msg.payload*1).toFixed(2);\n return msg; ","outputs":1,"noerr":0,"initialize":"","finalize":"","x":320,"y":220,"wires":[["b406cf79.f68bd","444c3494.0790ec"]]},{"id":"d0b7b024.a5c61","type":"ui_template","z":"faa8e95.a885c18","group":"5be80bfc.629394","name":"UI Table","order":2,"width":"6","height":"3","format":"<table style=\"width:100%\">\n <tr>\n <th>aadate</th> \n <th>bbdate</th> \n <th>ccdate</th>\n <th>date</th>\n </tr>\n <tr ng-repeat=\"x in msg.payload | limitTo:2\">\n <td>{{msg.payload[$index].currenttime}}</td>\n <td>{{msg.payload[$index].temperature}}</td> \n <td>{{msg.payload[$index].humidity}}</td>\n </tr>\n</table>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":false,"templateScope":"local","x":820,"y":440,"wires":[[]]},{"id":"6d2618c7.3560f8","type":"inject","z":"faa8e95.a885c18","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"900","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":380,"wires":[["30fb4ee.3c440b2"]]},{"id":"30fb4ee.3c440b2","type":"sqlite","z":"faa8e95.a885c18","mydb":"2c912755.393d98","sqlquery":"msg.topic","sql":"","name":"database1","x":350,"y":380,"wires":[["1e897b24.c714a5"]]},{"id":"444c3494.0790ec","type":"function","z":"faa8e95.a885c18","name":"","func":"var aa1 = msg.payload;\nflow.set(\"data\",aa1);\nvar newaa1 = msg.payload\nif (newaa1 != aa1)// If定時data不同於即時data\n {newaa1 = aa1 }// 即時data會取代定期data\n//問題是定期data每15分鐘一次, 如果在15分鐘內有變化,即時data如何傳入database?\nelse \n { newaa1 == msg.payload} \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":610,"y":220,"wires":[["9e9af451.5699d8","6b1cc7cb.f7c658"]]},{"id":"114328f5.edffd7","type":"comment","z":"faa8e95.a885c18","name":"take data from database","info":"// get data from database1 and send it to UI table","x":190,"y":320,"wires":[]},{"id":"a1e2c30d.41b5b","type":"comment","z":"faa8e95.a885c18","name":"store data to database","info":"data in > 0.00> 15min/1 or check data > store as bb1\ncheck data > data != newdata > data = newdata\nIf newdata = data > block unchanged number ","x":180,"y":120,"wires":[]},{"id":"b5edccd1.4ade7","type":"mqtt in","z":"faa8e95.a885c18","name":"","topic":"abc123","qos":"2","datatype":"auto","broker":"532a0205.3d9cfc","x":130,"y":180,"wires":[["553bdf96.1262"]]},{"id":"1e897b24.c714a5","type":"function","z":"faa8e95.a885c18","name":"get data every 15mins","func":"var values = flow.get([\"data\"]);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":600,"y":380,"wires":[["d0b7b024.a5c61","ea81ef4b.c6753"]]},{"id":"635751e4.1dd69","type":"sqlite","z":"faa8e95.a885c18","mydb":"2c912755.393d98","sqlquery":"msg.topic","sql":"","name":"database1","x":910,"y":220,"wires":[[]]},{"id":"ea81ef4b.c6753","type":"debug","z":"faa8e95.a885c18","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":810,"y":380,"wires":[]},{"id":"b14d0e3e.7bb1e","type":"comment","z":"faa8e95.a885c18","name":"show data in UI table","info":"","x":640,"y":460,"wires":[]},{"id":"f08a4770.76aa28","type":"inject","z":"faa8e95.a885c18","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"30","payloadType":"num","x":130,"y":260,"wires":[["553bdf96.1262"]]},{"id":"5be80bfc.629394","type":"ui_group","name":"Data","tab":"da0180c5.64fa9","order":3,"disp":true,"width":"6","collapse":false},{"id":"2c912755.393d98","type":"sqlitedb","db":"/home/pi/datebase1.db","mode":"RWC"},{"id":"532a0205.3d9cfc","type":"mqtt-broker","name":"OrgPi4-202","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"da0180c5.64fa9","type":"ui_tab","name":"IP Camera","icon":"photo_camera"}]

```

---

<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:** [26 January 2021 07:29 UTC](https://discourse.nodered.org/t/whats-wrong-with-msg-topic-the-query-is-not-defined-as-a-string/39756/4 "2021-01-26T07:29:37Z")

</div>

Hi, in order to make code more readable and importable it is important to surround your code with three backticks  
`````  
`like this`  
`````

You can edit and correct your post by clicking the pencil icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<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:** [26 January 2021 07:33 UTC](https://discourse.nodered.org/t/whats-wrong-with-msg-topic-the-query-is-not-defined-as-a-string/39756/5 "2021-01-26T07:33:40Z")

</div>

Also add a debug node showing what is going into the db node and check that msg.topic contains the correct query. Set the debug node to show Complete Message.

---

<div class="post-metadata">

**Author:** ![Arthur123124](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Arthur123124](https://discourse.nodered.org/u/Arthur123124)\
**Post date:** [26 January 2021 07:34 UTC](https://discourse.nodered.org/t/whats-wrong-with-msg-topic-the-query-is-not-defined-as-a-string/39756/6 "2021-01-26T07:34:36Z")

</div>

Thanks for helping.i will add a debug.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [26 January 2021 10:32 UTC](https://discourse.nodered.org/t/whats-wrong-with-msg-topic-the-query-is-not-defined-as-a-string/39756/7 "2021-01-26T10:32:50Z")

</div>

You must create your sql query and put it in msg.topic.

Nowhere in your flow do I see a valid sql query - Do you know how to format an sql 'insert' statement?

If not, you might want to do some reading.

---

<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:** [27 March 2021 10:33 UTC](https://discourse.nodered.org/t/whats-wrong-with-msg-topic-the-query-is-not-defined-as-a-string/39756/8 "2021-03-27T10:33:43Z")

</div>

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