Push in msg.payload

1,744 views
Skip to first unread message

Folgore

unread,
Apr 8, 2018, 5:28:44 PM4/8/18
to Node-RED
Hi, I  have a msg.payload[object], I want to push another object but receive the error. How should i do?

Thank's

Nick O'Leary

unread,
Apr 8, 2018, 5:39:18 PM4/8/18
to node...@googlegroups.com
Hi,

It is not entirely clear what you mean with the question.

If you are hitting an error, please share the actual error message and any relevant code you are using.

Nick

On Sun, 8 Apr 2018, 22:28 Folgore, <dario....@gmail.com> wrote:
Hi, I  have a msg.payload[object], I want to push another object but receive the error. How should i do?

Thank's

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/48aea8b6-6ea0-4664-b004-135e7a521f7f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Folgore

unread,
Apr 9, 2018, 1:32:30 AM4/9/18
to Node-RED
Sorry, I have this code in a function:
///////////////////////////////////////////////////////
var ex=global.get("ex");
var id=global.get("index");
var msg2= [];
var msg3=[]

msg3[id].push({key:"Sonno"+id,values:ex.Sheets.sonno['E'+id].v});
msg.payload=msg3;
msg.topic="Sleep"+id;
if (id<10)
{
    id=id+1;
    global.set("index",id);
    return msg;
}  
///////////////////////////////////////////////////////
I have create a for loop externally, but in this case is not possible to set msg.payload.push(msg3[id]), return a error (push is not a function). 

Thanks

Colin Law

unread,
Apr 9, 2018, 2:07:04 AM4/9/18
to node...@googlegroups.com
Firstly you have defined msg2 and 3 as empty arrays, messages must be objects not arrays. Secondly, push must be called on an array, but you are calling it on msg3[id] which does not exist and therefore is not an array.

Colin

Folgore

unread,
Apr 9, 2018, 2:28:51 AM4/9/18
to Node-RED
Yes, is an array of object, I want to add msg3 to msg.payload [object,object]. 

the result is similat to:

msg.payload={key:"Sonno1"+id,values:500}{key:"Sonno2"+id,values:650}.....excetera

You have a suggestion?

cflurin

unread,
Apr 9, 2018, 3:55:35 AM4/9/18
to Node-RED

Colin Law

unread,
Apr 9, 2018, 4:04:01 AM4/9/18
to node...@googlegroups.com
On 9 April 2018 at 07:28, Folgore <dario....@gmail.com> wrote:
> Yes, is an array of object, I want to add msg3 to msg.payload
> [object,object].
>
> the result is similat to:
>
> msg.payload={key:"Sonno1"+id,values:500}{key:"Sonno2"+id,values:650}.....excetera

Do you mean msg.payload is already an array of objects and you want to get
msg.payload=[{key:"Sonno1"+id,values:500},{key:"Sonno2"+id,values:650}, ...]

If so then you should be pushing to msg.payload
msg.payload.push({key:"Sonno"+id,values:ex.Sheets.sonno['E'+id].v}});

However that assumes the payload is coming in already as an array, if
not then you need to set it as an empty array first or just do
msg.payload = [{key:"Sonno"+id,values:ex.Sheets.sonno['E'+id].v}}]

I don't see how you are adding extra elements though, unless somehow
the message is being fed back in to the node with the accumulated
entries. I don't understand fully what you are trying to do but I
wonder if actually you should be saving the array in the node context
so you can add to it each time.

A couple of other points, don't use msg for a variable name unless it
is a message, that is just confusing. Also I suggest not using global
context unless you really need to. Use the node context if you can or
pass values in messages.

Colin

Folgore

unread,
Apr 9, 2018, 4:20:11 AM4/9/18
to Node-RED
I publish the code

[{"id":"99381905.1877d8","type":"inject","z":"ad25b718.72c0e8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":800,"wires":[["b43aa3ea.cf4d9"]]},{"id":"651b9c6e.99cc14","type":"debug","z":"ad25b718.72c0e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1210,"y":820,"wires":[]},{"id":"b43aa3ea.cf4d9","type":"file in","z":"ad25b718.72c0e8","name":"","filename":"C:\\\\telegram\\\\expor.xls","format":"","chunk":false,"sendError":false,"x":430,"y":800,"wires":[["65db7c87.3d9f04"]]},{"id":"65db7c87.3d9f04","type":"book","z":"ad25b718.72c0e8","name":"","x":660,"y":820,"wires":[["f159a181.895a7","51e4ca82.bb40c4","2fc9c371.eece7c"]]},{"id":"8a20d91d.981958","type":"ui_chart","z":"ad25b718.72c0e8","name":"","group":"1df8e342.92c5cd","order":0,"width":0,"height":0,"label":"chart","chartType":"bar","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"x":1270,"y":940,"wires":[[],[]]},{"id":"f159a181.895a7","type":"debug","z":"ad25b718.72c0e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":910,"y":800,"wires":[]},{"id":"c4452c7f.8e923","type":"debug","z":"ad25b718.72c0e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1230,"y":880,"wires":[]},{"id":"1e63a3b.9ab775c","type":"inject","z":"ad25b718.72c0e8","name":"","topic":"","payload":"index","payloadType":"global","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":590,"y":1080,"wires":[["8b838179.a65bb"]]},{"id":"51e4ca82.bb40c4","type":"function","z":"ad25b718.72c0e8","name":"","func":"var ex=msg.payload;\nvar msg2 = [];\nfor(i=2;i<10;i++)\n{\n\n//msg.payload=i;\nmsg2.push({key:\"Sonno\",values:ex.Sheets.sonno['E'+i].v});\n}\nmsg.payload=msg2;\n//msg.payload = [ { key: \"Power\", values : output} ];\nmsg.topic = \"Sonno\";\nreturn msg;\n","outputs":1,"noerr":0,"x":810,"y":880,"wires":[["c4452c7f.8e923","8a20d91d.981958"]]},{"id":"8b838179.a65bb","type":"debug","z":"ad25b718.72c0e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1290,"y":1080,"wires":[]},{"id":"bd55bd09.b99a4","type":"delay","z":"ad25b718.72c0e8","name":"","pauseType":"delay","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":790,"y":1000,"wires":[["8b838179.a65bb","f8dc0b18.815958","3cbcce21.df9bb2"]]},{"id":"3cbcce21.df9bb2","type":"ui_chart","z":"ad25b718.72c0e8","name":"","group":"1df8e342.92c5cd","order":0,"width":0,"height":0,"label":"test","chartType":"horizontalBar","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"x":1270,"y":1020,"wires":[[],[]]},{"id":"f8dc0b18.815958","type":"function","z":"ad25b718.72c0e8","name":"","func":"var ex=global.get(\"ex\");\nvar id=global.get(\"index\");\nvar msg2= [];\nvar msg3=[]\n\nmsg3[id].push({key:\"Sonno\"+id,values:ex.Sheets.sonno['E'+id].v});\nmsg.payload=msg3;\nmsg.topic=\"Sonno\"+id;\nif (id<10)\n{\n    id=id+1;\n    global.set(\"index\",id);\n    return msg;\n}  \n","outputs":1,"noerr":0,"x":770,"y":1040,"wires":[["bd55bd09.b99a4"]]},{"id":"2fc9c371.eece7c","type":"function","z":"ad25b718.72c0e8","name":"","func":"global.set(\"ex\",msg.payload)\nglobal.set(\"index\",2)\nreturn msg;","outputs":1,"noerr":0,"x":610,"y":1040,"wires":[["f8dc0b18.815958"]]},{"id":"eb8eb045.3406c","type":"comment","z":"ad25b718.72c0e8","name":"TYPE 1 ","info":"","x":850,"y":840,"wires":[]},{"id":"90b73632.ee7ee8","type":"comment","z":"ad25b718.72c0e8","name":"TYPE 2","info":"","x":850,"y":960,"wires":[]},{"id":"1df8e342.92c5cd","type":"ui_group","z":"","name":"Default Group","tab":"e758c8bc.1dc8b8","disp":true,"width":"16"},{"id":"e758c8bc.1dc8b8","type":"ui_tab","z":"","name":"Home Tab","icon":"dashboard"}]

the comment to Type 2  is the code with problem


Il giorno domenica 8 aprile 2018 23:39:18 UTC+2, Nick O'Leary ha scritto:

Colin Law

unread,
Apr 9, 2018, 4:33:15 AM4/9/18
to node...@googlegroups.com
I am afraid that flow is just horribly wrong. If you did remove the
bug from the function node it is going to send its message, that will
go into the delay, it will go back into the function node, into the
delay etc and will go round and round for ever.

Colin
> https://groups.google.com/d/msgid/node-red/d17e48e8-cb59-4f50-9179-1acc5375bd1c%40googlegroups.com.

Folgore

unread,
Apr 9, 2018, 4:54:41 AM4/9/18
to Node-RED
Yes, I know but is only a test for watch the performance.
The message is stopped when id is 10. Is not for ever. 

Folgore

unread,
Apr 9, 2018, 4:59:14 AM4/9/18
to Node-RED
Hi, do you mean this?

msg3[id].push([{key:"Sonno"+id,values:ex.Sheets.sonno['E'+id].v}]);
msg.payload.push=msg3;

I'm returned this error:


"TypeError: Cannot read property 'push' of undefined"

Colin Law

unread,
Apr 9, 2018, 5:03:37 AM4/9/18
to node...@googlegroups.com
On 9 April 2018 at 09:59, Folgore <dario....@gmail.com> wrote:
> Hi, do you mean this?
>
> msg3[id].push([{key:"Sonno"+id,values:ex.Sheets.sonno['E'+id].v}]);

Is msg3[id] an array? If not then you cannot push to it. Push adds
another element to an existing array. In fact msg3 is an empty array
so msg3[id] does not exist. I think you might save both of us a lot
of time in the long run if you were to take a few hours out and work
through some primers in javascript, in particular using objects and
arrays.

Colin
> --
> http://nodered.org
>
> Join us on Slack to continue the conversation: http://nodered.org/slack
> ---
> You received this message because you are subscribed to the Google Groups
> "Node-RED" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to node-red+u...@googlegroups.com.
> To post to this group, send email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/2947f33f-410d-4d16-80e4-b85024dc7915%40googlegroups.com.

Folgore

unread,
Apr 9, 2018, 5:32:59 AM4/9/18
to Node-RED
I waw wrong the syntax:

msg.payload.push(msg3);

However , I try  to set a msg,payload with msg3 (msg3 is an array) for the first time

var ex=global.get("ex");
var id=global.get("index");
var msg3=[]

msg3[0].push([{key:"Sonno"+id,values:ex.Sheets.sonno['E'+id].v}]);

if (id==2)
{
    msg.payload=msg3;
else
{
    msg.payload.push(msg3[0]);
msg.topic="Sonno"+id;
if (id<10)
{
    id=id+1;
    global.set("index",id);
    return msg;
}  

Now I received this error.

"TypeError: Cannot read property 'push' of undefined"

Nick O'Leary

unread,
Apr 9, 2018, 5:44:13 AM4/9/18
to Node-RED Mailing List
Hi,

you still have the issue Colin highlighted in his first reply:

var msg3=[]
msg3[0].push([{key:"Sonno"+id,values:ex.Sheets.sonno['E'+id].v}]);

You are defining msg3 as an empty array - so msg3[0] does not exist - it is undefined.

To push something into the msg3 array, you should do:
   
   msg3.push([{key:"Sonno"+id,values:ex.Sheets.sonno['E'+id].v}]);



Nick








--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Folgore

unread,
Apr 9, 2018, 11:15:03 AM4/9/18
to Node-RED
Ok, now I understand my error

Thank you very much
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
Reply all
Reply to author
Forward
0 new messages