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
- What platform?
- What OS you are using?
- What ersion of NR?
- 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
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:
{
"id":"894fec71.7d972",
"type":"function",
"z":"dd09b1ba.0e51e",
"name":"Weather Prediction",
"func":"var wBees = 0;\nvar wActually = msg.payload.newWt;\n\nvar c0 = msg.payload.data[0].CO2;\nvar c1 = msg.payload.data[1].CO2;\nvar c2 = msg.payload.data[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: \" + msg.payload.weather;\n\treturn msg;\n}else{\n\tmsg.payload.weatherPredict = \"Incorrect\" + \" || Current: \" + msg.payload.weather;\n\treturn msg;\n}",
"outputs":1,
"noerr":0,
"x":830,
"y":360,
"wires":[
[
"3ee84278.8a86de"
]
]
}
]
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 = msg.payload.data[0].CO2;
//var c1 = msg.payload.data[1].CO2;
//var c2 = msg.payload.data[2].CO2;
//wActually.localeCompare("rain") === false
if(wActually === "rain"){
wActually = "good";
}else{
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";
}else{
// good weather
wBees = "good";
}
// wait 24hrs
}
// Evaluation
if(wBees == wActually){
msg.payload.weatherPredict = "Correct" + " || Current: " + msg.payload.weather;
return msg;
}else{
msg.payload.weatherPredict = "Incorrect" + " || Current: " + msg.payload.weather;
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
2 Likes
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.