2
votes

I am using node-red, the function node to generate some random matrix. However, every time when I deploy the node, the error "TypeError: Cannot read property 'push' of undefined" will appear, can someone help me to take a look at the code?

// Generate some random data
// See https://www.patrick-wied.at/static/heatmapjs/example-minimal-config.html

var len = 200;

msg.payload = [];

while (len--) {
  var value = Math.floor(Math.random() * 100);
  msg.payload.push(value);
}

return msg;
2
This code, when used inside a Node-RED function node, works perfectly. Perhaps you are getting this error from some other part of your flow? It would be easier to investigate if you share the relevant part of the flow. - AIOT MAKER

2 Answers

4
votes

The error tells , push cannot be done on 'undefined'. This suggests that msg is undefined or msg.payload is undefined. In your case, declare the payload separately before push and then assign it to the msg finally.

0
votes

Initialise msg before

var len = 200;
var msg = {};
msg.payload = [];