# Code function to collect data from mqtt

**URL:** https://discourse.nodered.org/t/code-function-to-collect-data-from-mqtt/54588
**Category:** General
**Tags:** database, function-node
**Created:** [1 December 2021 16:04 UTC](https://discourse.nodered.org/t/code-function-to-collect-data-from-mqtt/54588 "2021-12-01T16:04:32Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![huuthang911](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/huuthang911/32/50601_2.png) [@huuthang911](https://discourse.nodered.org/u/huuthang911)
#### Post date: [1 December 2021 16:04 UTC](https://discourse.nodered.org/t/code-function-to-collect-data-from-mqtt/54588/1 "2021-12-01T16:04:32Z")

</div>

Hi folks,  
I have 3 topics in MQTT. I want to use the data of these topics but I don't know how to write code in the function node. Can you suggest to me the code to declare variables in the function node.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/c/2ce44ebbd81c9f470d75e9bab13a120cef15402e.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: [1 December 2021 16:06 UTC](https://discourse.nodered.org/t/code-function-to-collect-data-from-mqtt/54588/2 "2021-12-01T16:06:58Z")

</div>

If you want all 3 values together for sending to database, you need to use a join node.

See [this article in the cookbook](https://cookbook.nodered.org/basic/join-streams) for an example of how to join messages into one object.

Once you have the values together, you can then start focusing on the database part.

When you come back, be sure to mention the database type, what nodes you are using and what you have tried so far.

---

<div class="post-metadata">

### Author: ![huuthang911](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/huuthang911/32/50601_2.png) [@huuthang911](https://discourse.nodered.org/u/huuthang911)
#### Post date: [1 December 2021 16:20 UTC](https://discourse.nodered.org/t/code-function-to-collect-data-from-mqtt/54588/3 "2021-12-01T16:20:57Z")

</div>

I want to declare variables to use each variable to put it to next program in function node

---

<div class="post-metadata">

### Author: ![huuthang911](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/huuthang911/32/50601_2.png) [@huuthang911](https://discourse.nodered.org/u/huuthang911)
#### Post date: [2 December 2021 03:19 UTC](https://discourse.nodered.org/t/code-function-to-collect-data-from-mqtt/54588/4 "2021-12-02T03:19:56Z")

</div>

for example to declare variables like  
var temperature = \<\>;  
I don't know how to arrange topic and the payload in the code

---

<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: [2 December 2021 08:34 UTC](https://discourse.nodered.org/t/code-function-to-collect-data-from-mqtt/54588/5 "2021-12-02T08:34:31Z")

</div>

There is a great page in the docs that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

![BX00Cy7yHi](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

[https://nodered.org/docs/user-guide/messages](https://nodered.org/docs/user-guide/messages)

Also 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 in about 1 hour. A small investment for a lot of gain.

---

<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 January 2022 08:35 UTC](https://discourse.nodered.org/t/code-function-to-collect-data-from-mqtt/54588/6 "2022-01-31T08:35:17Z")

</div>

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