# Flow set "array error" using mqtt topic

**URL:** <https://discourse.nodered.org/t/flow-set-array-error-using-mqtt-topic/50973>\
**Category:** General\
**Created:** [13 September 2021 16:18 UTC](https://discourse.nodered.org/t/flow-set-array-error-using-mqtt-topic/50973 "2021-09-13T16:18:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lost](https://avatars.discourse-cdn.com/v4/letter/l/22d042/32.png) [@Lost](https://discourse.nodered.org/u/Lost)\
**Post date:** [13 September 2021 16:18 UTC](https://discourse.nodered.org/t/flow-set-array-error-using-mqtt-topic/50973/1 "2021-09-13T16:18:55Z")

</div>

Hi.

I am trying to save a thermostat mode to a flow variable using the msg.topic but it does not work as expected.

Flow:

 ![flow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/4/348689c3ad90e77eb1934834d8e286e63c4daf7b.jpeg)

Result:

 ![result](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/7/97761edb317aad04809e6898132989b674eb0e5e.jpeg)

How can i save it based on the MQTT adress without getting all those array enteries as well?

This is my example flow:

```auto
[{"id":"d8f143e9.eb063","type":"function","z":"82add03.b9a433","name":"","func":"//Get adress and build flow variable name \nvar address = msg.topic.split(\"/\")[2];\nvar newname = \"hdl.fh.\" + address + \".textmode\"\n\n//Save msg to flow variable\nflow.set (newname, msg.payload)\n\n//Debug\nmsg.address = address\nmsg.newname = newname\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":120,"wires":[["ca252047.e559a"]]},{"id":"ca252047.e559a","type":"debug","z":"82add03.b9a433","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":530,"y":120,"wires":[]},{"id":"dbbd3792.f6fd88","type":"inject","z":"82add03.b9a433","name":"Test input msg: Normal","props":[{"p":"topic","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"hdl/fh/1.106/textmode/set","payload":"normal","payloadType":"str","x":140,"y":80,"wires":[["d8f143e9.eb063","2bfddbe0.c8dea4"]]},{"id":"de39039.e93a9","type":"inject","z":"82add03.b9a433","name":"Test input msg: Day","props":[{"p":"topic","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"hdl/fh/1.106/textmode/set","payload":"day","payloadType":"str","x":130,"y":140,"wires":[["d8f143e9.eb063","2bfddbe0.c8dea4"]]},{"id":"2bfddbe0.c8dea4","type":"debug","z":"82add03.b9a433","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":350,"y":200,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)\
**Post date:** [13 September 2021 18:24 UTC](https://discourse.nodered.org/t/flow-set-array-error-using-mqtt-topic/50973/2 "2021-09-13T18:24:50Z")

</div>

It's `flow.set("newname", msg.payload)`  
With quotes around the variable name, same with flow.get

---

<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:** [13 September 2021 18:39 UTC](https://discourse.nodered.org/t/flow-set-array-error-using-mqtt-topic/50973/3 "2021-09-13T18:39:17Z")

</div>

You issue is the numbers in the path are being interpreted as array elements

You will need to DIY this one...

```javascript
const data = {
    hdl: flow.get("hdl") || {}
}

//Get adress and build flow variable name 
const address = msg.topic.split("/")[2];
const newname = "hdl.fh." + address + ".textmode";
const value = msg.payload;

setValue(data, newname, value);
flow.set("hdl", data.hdl);

//Debug
msg.address = address
msg.newname = newname

return msg;

/**
* @param {object} obj
* @param {string} path
* @param {any} value
*/
function setValue(obj, path, value) {
    var a = path.split('.')
    var o = obj
    while (a.length - 1) {
        var n = a.shift()
        if (!(n in o)) o[n] = {}
        o = o[n]
    }
    o[a[0]] = value
}

```

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

---

<div class="post-metadata">

**Author:** ![Lost](https://avatars.discourse-cdn.com/v4/letter/l/22d042/32.png) [@Lost](https://discourse.nodered.org/u/Lost)\
**Post date:** [13 September 2021 20:34 UTC](https://discourse.nodered.org/t/flow-set-array-error-using-mqtt-topic/50973/4 "2021-09-13T20:34:24Z")

</div>

> [@Steve-Mcl](#):
>
> You will need to DIY this one...

Thanks that worked great. I would never be able to get that working alone, and still dont understand how that code works.

---

<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:** [13 September 2021 20:36 UTC](https://discourse.nodered.org/t/flow-set-array-error-using-mqtt-topic/50973/5 "2021-09-13T20:36:57Z")

</div>

In simple terms, the `setValue` splits `hdl.fh.1.106.textmode` by "." into an array then loops through them setting the object `obj` parameters, creating a new object where none exists & on the last element, setting the value.

---

<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 September 2021 20:37 UTC](https://discourse.nodered.org/t/flow-set-array-error-using-mqtt-topic/50973/6 "2021-09-27T20:37:35Z")

</div>

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