Help in function node - for each - flow context (moving a msg.payload to flow.context in a function node)

I am trying to set an array into flow context but unable to write the appropriate code.
please help. I get only null. i dont know whcih part of the code in function node is incorrect.

const data = Array.isArray(msg.payload) ? msg.payload : []
const out = []
data.forEach(i => {if (i.payload) out.push(i.payload)
    else out.push(null)})
flow.set('abc', out) 
return msg;
[{"id":"ddec360045e28541","type":"inject","z":"cd84b99c9e598453","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":390,"y":4320,"wires":[["3b7ac0241e41b171"]]},{"id":"3b7ac0241e41b171","type":"change","z":"cd84b99c9e598453","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"[\"▼ 15.2 Day\",\"▼ 44.2 Day\",\"▼ 17 Day\",\"▲ 24 Min\",\"▲ 50 Min\",\"▼ 4.8 Day\",\"▼ 4.8 Day\",\"▼ 55 Min\",\"▼ 1 Min\",\"▼ 3.5 Day\",\"▼ 226 Min\",\"▼ 20 Min\",\"▲ 242.8 Day\",\"▲ 242.8 Day\",\"▲ 288.8 Day\",\"▼ 9.9 Day\",\"▼ 72 Min\",\"▲ 242.9 Day\",\"▲ 18 Min\",\"▼ 60 Min\",\"▼ 5.4 Day\",\"▼ 53 Min\",\"▼ 52 Min\",\"▼ 552 Min\"]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":4320,"wires":[["ad927c125cf61172","137c9bbf5fa3b9e0"]]},{"id":"ad927c125cf61172","type":"function","z":"cd84b99c9e598453","name":"flow context","func":"const data = Array.isArray(msg.payload) ? msg.payload : []\nconst out = []\ndata.forEach(i => {if (i.payload) out.push(i.payload)\n    else out.push(null)})\nflow.set('abc', out) \nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":790,"y":4320,"wires":[["777259245f6db068"]]},{"id":"777259245f6db068","type":"debug","z":"cd84b99c9e598453","name":"debug 16","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":980,"y":4320,"wires":[]},{"id":"137c9bbf5fa3b9e0","type":"debug","z":"cd84b99c9e598453","name":"debug 17","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":760,"y":4380,"wires":[]}]

Stick a console.log in the for-each and see what it shows.

I could eventually get what I wanted, there was no need
for i looks like, but I dont know whether it is the right way..

so the input may not be what i thought it is. array/object/array of objects/ i get lost in the semantics.

const data = Array.isArray(msg.payload) ? msg.payload : []
const out = []
if (msg.payload) {out.push(msg.payload)}
else 
{out.push(null)}
flow.set('abc', out) 
return msg;

I would usually do:

if (!msg.payload) msg.payload = []
if (!Array.isArray(msg.payload)) msg.payload = [msg.payload]

But:

if (msg.payload) {out.push(msg.payload)}
else 
{out.push(null)}
flow.set('abc', out) 

Seems odd. With my above code, all you need is:

if (!msg.payload) msg.payload = []
if (!Array.isArray(msg.payload)) msg.payload = [msg.payload]
flow.set('abc', msg.payload) 
return msg
1 Like

I have just copied and adapted the solution given to me sometime back for a different problem, honestly i dont know what each line does.
I just added your solution to the flow and it works!

my solution was giving abc[0][0], and yours gives abc[0]

i will now have to revise my downstream flow to change from [0][0] to [0],

thanks for the help.

I always get confused around usage of [0], .0 etc, when to use what

Haha, wait till you introduce arrays of objects, objects of arrays and objects of objects!

At least you aren't dealing with the crazy complex terminology using in Python!

I've been dealing with oddly shaped hierarchical and multi-dimensional data since the early days of my career back in the early 1980's and I still sometimes get confused.


myvar[x][y] implies an array of arrays, e.g.

[
   [1, 2, 3],
   ['a', 'b', 'c']
]

myvar[1][2] = c. (offsets are zero based)


myvar.x.y implies an object of objects, eg.

{
  one: { a:1, b:2, c:3 },
  two: { z:9, x:99, y:999 }
}

myvar.two.x = 99 = myvar['two']['x']

Brackets in object notation are good for when you either have a property that isn't valid when used in dot notation, e.g., you can't do myvar.some-prop or myvar.99redballoons. Or if you want to use a variable to access an object property: const v = 'x'; console.log( myvar.two[v] ).


JavaScript lets you go to very deep hierarchies but anything over 3 or 4 should be avoided for the sake of everyone's sanity. :slight_smile:

JavaScript even lets objects be recursive as you will see if you ever dump out an ExpressJS request or response object.

1 Like