Multiple inputs in function

I'm trying to handle multiple msg.payload.subobjects to a function node. But when doing so I always get the error ""TypeError: Cannot read property '0' of undefined" ". When I try to get the data with an debug node, everything seems fine. But handling them to a function gives me that error.

Json is in a post beneath.

Thanks for your help! Pretty new to node-red.

Welcome to the forum! I see you have six function nodes. Do you know where the error comes from?

add a catch node connected to a debug node (set to display the Complete msg object) on the page and see what that shows.

Also when opening a thread it's a good ides to let people know

  1. What platform?
  2. What OS you are using?
  3. What ersion of NR?
  4. What version of node.js?

and in this case, I would add a debug node to the output of the openweathermap node to capture what it provide. You could then use a inject/change node to build a copy of what you are getting from the openweathermap. This would allow someone willing to give you some of their time the ability to test your code without doing extra steps, like creating an account to beable to get the data.

You want to make it as easy as possible for someone to help you :grinning:

The error occurs in the "weather prediction" node.

OS is Windows 10
Node-Red Version is 1.0.3
node.js is v12.14.0

I would add a debug node to the output of the openweathermap node to capture what it provide.

I did. It's providing the correct weather data. After the "convert" nodes also debug nodes provide the correct STRING data as I want. But passing them into the "weather prediction" function returns the error.

Is it possible to handle two inputs like that to a function? Cause they both use the msg.payload object?

You want to make it as easy as possible for someone to help you

Here's the prepared version for testing:

      "name":"Weather Prediction",
      "func":"var wBees = 0;\nvar wActually = msg.payload.newWt;\n\nvar c0 =[0].CO2;\nvar c1 =[1].CO2;\nvar c2 =[2].CO2;\n\n//wActually.localeCompare(\"rain\") === false\nif(wActually === \"rain\"){\n    wActually = \"good\";\n}else{\n    wActually = \"other\";\n}\n\n//c0, c1, c2 --- 2pm, 10pm, 6am\n  var day = c1-c0;\n  var night = c1-c2;\n\n// Checks proportion\n  if(night > day){\n    // bad weather\n\twBees = \"other\";\n  }else{\n    // good weather\n\twBees = \"good\";\n  }\n\n// Evaluation\nif(wBees == wActually){\n    msg.payload.weatherPredict = \"Correct\" + \" || Current: \" +;\n\treturn msg;\n}else{\n\tmsg.payload.weatherPredict = \"Incorrect\" + \" || Current: \" +;\n\treturn msg;\n}",

Yes and no. yes you can have multiple inputs into a function node BUT each one will run separately. If you need data fron the other one, you will need to join them together before sending to the function node.

However that is not what is causing the error. Lets look at your code

var wBees = 0;
var wActually = msg.payload.newWt;

var c0 =[0].CO2;
//var c1 =[1].CO2;
//var c2 =[2].CO2;

//wActually.localeCompare("rain") === false
if(wActually === "rain"){
    wActually = "good";
    wActually = "other";

//c0, c1, c2 --- 2pm, 10pm, 6am
predictWeatherByBees = (c0, c1, c2) => {
  var day = c1-c0;
  var night = c1-c2;

// Checks proportion
  if(night > day){
    // bad weather
	wBees = "other";
    // good weather
	wBees = "good";
  // wait 24hrs

// Evaluation
if(wBees == wActually){
    msg.payload.weatherPredict = "Correct" + " || Current: " +;
	return msg;
	msg.payload.weatherPredict = "Incorrect" + " || Current: " +;
	return msg;

You have the line predictWeatherByBees = (c0, c1, c2) => {...where are c0, c1 and c2 coming from?

You also might want to read about joins Working with messages : Node-RED and here is an example Create a single message from separate streams of messages : Node-RED

1 Like

Oh, you're right. That line is an old one. But removing the function and just running the code without it still calls the error.

Guess I'll try the join then. That might be the problem.