DragonPig
DragonPig

Reputation: 65

TypeError: Cannot read property 'push' of undefined in node-red function node

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;

Upvotes: 2

Views: 1668

Answers (2)

Pavithra Ravichandran
Pavithra Ravichandran

Reputation: 55

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.

Upvotes: 4

Anup
Anup

Reputation: 629

Initialise msg before

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

Upvotes: 0

Related Questions