twitter out msg : error TypeError: Cannot read property 'length' of undefined

326 views
Skip to first unread message

Louanne Dante

unread,
Jul 18, 2016, 8:14:52 AM7/18/16
to Node-RED
Hello dear community,
i am about to write my bachelor-thesis about node-RED and i have quite an urgent issue.
I have a flow which should tweet the weather for the day when the flow is triggered, but the twitter out node says:


msg : error 
TypeError: Cannot read property 'length' of undefined


















The Flow requests weather data as a Javascript-Object from the yahoo weather API with a http-request node.
Then, in a function node I extract the weather data for today into a new msg1:

var forecast = msg.payload.query.results.channel.item.forecast;
var weather_today = vorhersage[0].text; 
msg1 = {payload: weather_today, topic: "today"}; 
return msg1;

Next, I use a switch node that sends the message to different outputs, depending on the words that the payload contains.
For example, "when the payload contains 'Sun', send it to output 1, when it contains 'Rain', send it to output 2" and so on..

The different outputs of the switch node are connected to different function nodes, in which I generate different sentences, depending on the weather. These sentences should be tweetet later.
For example, when the msg.payload contains 'Sun', msg goes into the function node that generates sunny sentences like "Today its sunny in Berlin".
There are also function nodes that generate windy sentences and rainy sentences and so on.
I always want the tweets to be different. Thats why I increase (++) a global variable in a new function node after I generate the different tweet sentences. This happens everytime I trigger the flow ( button connected to the Raspi or simply via an Inject node).
Dependent on the number of the global variable I grab a different tweet out of the buffer everytime:

var tweet1 = "In Berlin it'll be sunny today.";
var tweet2 = "Today the sun is shining in Berlin.";
var tweet3 = "In Berlin the sun will shine today";
var tweet4 = "Today the sun is laughing in Berlin";
var tweet5 = "Berlin shows its sunny side today";

var tweetbuffer = [tweet1, tweet2, tweet3, tweet4, tweet5];
var change = context.global.flag;

msg.payload = tweetbuffer[change];
return msg;



I think that it is an easy error, but I dont know where to start. So I really would appreciate any help. Thank you!!


This is the flow for the clipboard:

[{"id":"f8281246.acd7","type":"rpi-gpio in","z":"999dee59.0a7eb","name":"Pin12 Button","pin":"12","intype":"up","debounce":"25","read":false,"x":76.50000762939453,"y":202.27777290344238,"wires":[["43cd0b38.d87c04"]]},{"id":"93ce9530.5a4388","type":"http request","z":"999dee59.0a7eb","name":"weather in Berlin","method":"GET","ret":"obj","url":"https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22berlin%2C%20ak%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys","x":269.00000762939453,"y":240.0277738571167,"wires":[["35a787a3.72abc8"]]},{"id":"43cd0b38.d87c04","type":"switch","z":"999dee59.0a7eb","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"str"},{"t":"eq","v":"1","vt":"str"}],"checkall":"true","outputs":2,"x":90.37500381469727,"y":290.2777738571167,"wires":[["93ce9530.5a4388"],[]]},{"id":"35a787a3.72abc8","type":"function","z":"999dee59.0a7eb","name":"extract weather","func":"var vorhersage = msg.payload.query.results.channel.item.forecast;\nvar wetter_heute = vorhersage[0].text; \n//var wetter_morgen = vorhersage[1].text;\n//var wetter_uebermorgen = vorhersage[2].text;\n\nmsg1 = {payload: wetter_heute, topic: \"heute\"}; \n//msg2 = {payload: wetter_morgen, topic: \"morgen\"};\n//msg3 = {payload: wetter_uebermorgen, topic: \"uebermorgen\"};\n\nreturn msg1;\n//return [msg1, msg2];","outputs":"1","noerr":0,"x":224.125,"y":366.5277738571167,"wires":[["ee836fd6.93406","2370f81.bb46608"]]},{"id":"ee836fd6.93406","type":"switch","z":"999dee59.0a7eb","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"Sun","vt":"str"},{"t":"cont","v":"Cloud","vt":"str"},{"t":"cont","v":"Breez","vt":"str"},{"t":"cont","v":"Rain","vt":"str"},{"t":"cont","v":"Shower","vt":"str"},{"t":"cont","v":"Thunder","vt":"str"},{"t":"else"}],"checkall":"true","outputs":7,"x":495.37500762939453,"y":437.7778043746948,"wires":[["296226cc.90287a"],["d8193b9a.e14a78"],["29aa2eef.d51f62"],["57026c15.a47fa4"],["57026c15.a47fa4"],["95bc9056.8b0e2"],["dc324935.4a2738"]]},{"id":"8142583f.abb4b8","type":"comment","z":"999dee59.0a7eb","name":"1: Sun 2: Cloud 3: Breeze 4: Rain 5: Shower 6: Thunder 7: anything else","info":"","x":710.2361602783203,"y":617.4999675750732,"wires":[]},{"id":"296226cc.90287a","type":"function","z":"999dee59.0a7eb","name":"sunny Tweets","func":"var tweet1 = \"In Berlin wird es heute sonnig.\";\nvar tweet2 = \"Heute scheint die Sonne in Berlin.\";\nvar tweet3 = \"In Berlin wird heut' die Sonne scheinen.\";\nvar tweet4 = \"In Berlin lacht heut' die Sonne.\";\nvar tweet5 = \"Berlin zeigt sich heut' von seiner sonnigen Seite.\";\n\nvar tweetbuffer = [tweet1, tweet2, tweet3, tweet4, tweet5];\nvar change = context.global.flag;\n\nmsg.payload = tweetbuffer[change];\nreturn msg;","outputs":1,"noerr":0,"x":687.8750114440918,"y":321.5277724266052,"wires":[["73e65fab.0a24b","c01cf2d7.55797"]]},{"id":"d8193b9a.e14a78","type":"function","z":"999dee59.0a7eb","name":"cloudy Tweets","func":"var tweet1 = \"Berlin wird heute von Wolken verdeckt.\";\nvar tweet2 = \"Heute wird es wolkig in Berlin.\";\nvar tweet3 = \"Berlin ist heute voll von Wolken.\";\nvar tweet4 = \"In Berlin ist es heute bedeckt.\";\nvar tweet5 = \"Berlin ist heute wolkenbedeckt.\";\n\nvar tweetbuffer = [tweet1, tweet2, tweet3, tweet4, tweet5];\n\nvar change = context.global.flag;\n\nmsg.payload = tweetbuffer[change];\n\nreturn msg;","outputs":1,"noerr":0,"x":691.6250114440918,"y":369.0277738571167,"wires":[["73e65fab.0a24b","c01cf2d7.55797"]]},{"id":"29aa2eef.d51f62","type":"function","z":"999dee59.0a7eb","name":"windy Tweets","func":"var tweet1 = \"In Berlin weht heut' der Wind.\";\nvar tweet2 = \"Heute wird's windig in Berlin.\";\nvar tweet3 = \"In Berlin kann man heute segeln gehen.\";\nvar tweet4 = \"Gutes Wetter zum Segeln in Berlin.\";\nvar tweet5 = \"In Berlin weht heut' ne steife Brise.\";\n\nvar tweetbuffer = [tweet1, tweet2, tweet3, tweet4, tweet5];\nvar change = context.global.flag;\n\nmsg.payload = tweetbuffer[change];\n\nreturn msg;","outputs":1,"noerr":0,"x":700.3750038146973,"y":411.5277738571167,"wires":[["73e65fab.0a24b","c01cf2d7.55797"]]},{"id":"57026c15.a47fa4","type":"function","z":"999dee59.0a7eb","name":"rainy Tweets","func":"var tweet1 = \"Heute wird's nass in Berlin.\";\nvar tweet2 = \"In Berlin wird es heute regnen.\";\nvar tweet3 = \"Berliner, vergesst heut' nicht den Regenschirm.\";\nvar tweet4 = \"Heute lieber Bahn fahren, es wird nass.\";\nvar tweet5 = \"In Berlin gibt's heute Regen.\";\n\nvar tweetbuffer = [tweet1, tweet2, tweet3, tweet4, tweet5];\n\nvar change = context.global.flag;\nmsg.payload = tweetbuffer[change];\nreturn msg;","outputs":1,"noerr":0,"x":686.6250038146973,"y":460.2777738571167,"wires":[["c01cf2d7.55797"]]},{"id":"95bc9056.8b0e2","type":"function","z":"999dee59.0a7eb","name":"stormy Tweets","func":"var tweet1 = \"In Berlin wird's heute gewittern.\";\nvar tweet2 = \"Heute gibt's Blitz und Donner in Berlin.\";\nvar tweet3 = \"In Berlin gibt's heut' ein Unwetter.\";\nvar tweet4 = \"Berliner, heute lieber zu Hause bleiben.\";\nvar tweet5 = \"In Berlin wird's heute ungemütlich.\";\n\nvar tweetbuffer = [tweet1, tweet2, tweet3, tweet4, tweet5];\nvar change = context.global.flag;\nmsg.payload = tweetbuffer[change];\nreturn msg;","outputs":1,"noerr":0,"x":695.3750076293945,"y":517.7777757644653,"wires":[["73e65fab.0a24b","c01cf2d7.55797"]]},{"id":"dc324935.4a2738","type":"function","z":"999dee59.0a7eb","name":"let's see Tweets","func":"var tweet1 = \"Mal sehen wie das Wetter heute wird.\";\nvar tweet2 = \"Wie wird das Wetter heute?\";\nvar tweet3 = \"Auf die Wettervorhersage ist (n)immer verlass.\";\nvar tweet4 = \"In Berlin ist's immer schön.\";\nvar tweet5 = \"Es gibt kein schlechtes Wetter, nur falsche Kleidung.\";\n\nvar tweetbuffer = [tweet1, tweet2, tweet3, tweet4, tweet5];\nvar change = context.global.flag;\n\nmsg.payload = tweetbuffer[change];\nreturn msg;","outputs":1,"noerr":0,"x":662.8750076293945,"y":565.2777757644653,"wires":[["73e65fab.0a24b","c01cf2d7.55797"]]},{"id":"2370f81.bb46608","type":"debug","z":"999dee59.0a7eb","name":"","active":true,"console":"false","complete":"payload","x":483.875,"y":278.77777004241943,"wires":[]},{"id":"c01cf2d7.55797","type":"function","z":"999dee59.0a7eb","name":"flag++","func":"if (context.global.flag == 4){\n    context.global.flag = 0;\n    return msg;\n}else{\n    context.global.flag++\n    return msg;\n}\n","outputs":1,"noerr":0,"x":1012.2639236450195,"y":487.86108207702637,"wires":[["43e3f128.c290f"]]},{"id":"73e65fab.0a24b","type":"debug","z":"999dee59.0a7eb","name":"","active":true,"console":"false","complete":"true","x":1102.9582786560059,"y":322.055552482605,"wires":[]},{"id":"43e3f128.c290f","type":"twitter out","z":"999dee59.0a7eb","twitter":"","name":"Tweet","x":1084.9998817443848,"y":595.1944169998169,"wires":[]},{"id":"2a9dd055.52799","type":"inject","z":"999dee59.0a7eb","name":"","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"x":84.33333333333333,"y":142.22222222222223,"wires":[["43cd0b38.d87c04"]]}]



Louanne Dante

unread,
Jul 18, 2016, 9:06:02 AM7/18/16
to Node-RED
Sorry, forgot something: 
The function node where i change the global variable looks like this:

if (context.global.flag == 4){
    context.global.flag = 0;
    return msg;
}else{
    context.global.flag++
    return msg;
}



Zenofmud

unread,
Jul 18, 2016, 11:00:44 AM7/18/16
to node...@googlegroups.com
backup your flow, then try changing that function to this

var f = global.get("flag");
if (f > 3){
    f = 0;
} else {
    f = f +1;
}
global.set("flag", f);
return msg;

--
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.
For more options, visit https://groups.google.com/d/optout.

Louanne Dante

unread,
Jul 18, 2016, 1:32:49 PM7/18/16
to Node-RED
Thanks for your quick response, Paul!
Unfortunately, your suggestion doesn't solve the problem.
But I just discovered, that after the function nodes where I generate the Tweet-Sentences, msg.payload turns out to be UNDEFINED. 
So then it is no surprise that the twitter out node won't work.
But I wonder why it is undefinded, as I clearly say:
msg.payload = tweetbuffer[change];
return msg;

It works fine when I say:
msg.payload = "test";
return msg;

There must be an issue with the buffer, but I dont know how to figure it out.

Louanne Dante

unread,
Jul 18, 2016, 2:33:02 PM7/18/16
to Node-RED

EDIT: Now i found out that there is an issue with catching the global variable... It is indicated as "null"....i will try further tomorrow 

Louanne Dante

unread,
Jul 18, 2016, 2:39:33 PM7/18/16
to Node-RED
Paul, sorry I first didn't understand your suggestion correctly at first. But now I think I do and there is an issue with catching the global variable. When I do it your way, the number is recognized as: NaN

Dave C-J

unread,
Jul 18, 2016, 2:54:51 PM7/18/16
to node...@googlegroups.com

The context.global.flag is never initialised in that function so the else can't then increment it. You need something like
context.global.flag =context.global.flag || 0;
To set it to 0 to start with if it's not already in existence.

Zenofmud

unread,
Jul 18, 2016, 4:29:49 PM7/18/16
to node...@googlegroups.com
Ahh yes, I when I was playing, I had an function to initialize it, sorry I forgot to mention that :>(
On Jul 18, 2016, at 2:39 PM, Louanne Dante <louann...@gmail.com> wrote:

Paul, sorry I first didn't understand your suggestion correctly at first. But now I think I do and there is an issue with catching the global variable. When I do it your way, the number is recognized as: NaN

Dave C-J

unread,
Jul 18, 2016, 5:14:30 PM7/18/16
to node...@googlegroups.com
Forgot to mention  or forgot to re-apply it ? the symptoms you have are of it not being initialised.

Louanne Dante

unread,
Jul 20, 2016, 6:29:14 AM7/20/16
to Node-RED
Thank you!!Unfortunately I have an exam on friday so I will try to figure it out after that:)

cflurin

unread,
Jul 21, 2016, 5:00:19 AM7/21/16
to Node-RED
An alternative solution without global (sample for cloudy Tweets):
var msg1 = {};
var tweet;
var tweet_number = Math.floor(Math.random() * 4); // 0 .. 4

switch (tweet_number) {
    case 0: tweet = "Berlin wird heute von Wolken verdeckt."; break;
    case 1: tweet = "Heute wird es wolkig in Berlin."; break;
    case 2: tweet = "Berlin ist heute voll von Wolken."; break;
    case 3: tweet = "In Berlin ist es heute bedeckt."; break;
    case 4: tweet = "Berlin ist heute wolkenbedeckt."; break;
    default: tweet = "no tweet";
}

msg1.payload = tweet;
return msg1;

Louanne Dante

unread,
Jul 27, 2016, 8:55:15 AM7/27/16
to Node-RED
thank you cflurin,
i got it working now with this:

1 var global = flow.get('save')||0;

2

3 if (global > 3){

4     flow.set('save','0');

5     msg.count= 0;

6     return msg;

7     } else{

8         global++;

9         flow.set('save', global);

10       }

11

12 msg.count = global;

13 return msg;

 

and then i caught the global variable in another function node with this:


1 var tweet0 = "Berlin wird heute von Wolken verdeckt.";

2 var tweet1 = "Heute wird es wolkig in Berlin.";

3 var tweet2 = "Berlin ist heute voll von Wolken.";

4 var tweet3 = "In Berlin ist es heute bedeckt.";

5 var tweet4 = "Berlin ist heute wolkenbedeckt.";

6 var add = " Die Höchsttemperatur liegt bei " + msg.temp +" °C.";

7 var tweetbuffer = [tweet0+add, tweet1+add, tweet2+add, tweet3+add, tweet4+add];

8

9 var zaehle = flow.get('save');

10 msg.payload = tweetbuffer[zaehle];

11 return msg;



Thank you all, this problem is solved
Reply all
Reply to author
Forward
0 new messages