Please - Why won't this work

I just can't get this to work.
Thanks in advance
Cheers

// All the vars below are of type string

var reg = msg.payload.registration;
var actype = msg.payload.type;
var alt = String(msg.payload.altitude);
var squawk = msg.payload.squawk;
var time = msg.payload.time;
var hdg = String(msg.payload.hdg);
var message="";

msg.topic = reg + " Overflew @ " + time;
message = actype + " " + reg + " Overflew @ " + time +" Altitude " + alt +" HDG " + hdg + " Sqk " + squawk;

outputmsg = {
payload: message
};

return [outputmsg];

I get this output
image

Show us what a debug node showing the input to the function contains. The output suggests that, for example, msg.payload.type does not exist.

For future reference, when pasting code, logs, flows etc please use the </> button at the top of the text entry window to format it correctly.

Hi Colin,
this is what is going in

Cheers.
Paul.

image

If you look carefully you will see there is no msg.payload.type, there is a msg.payload.Type though. Does that help?

2 Likes

Hi Colin,
Thank you for your attention to detail.
Solved the problem by changing them all!

Cheers.
Paul.

In case you are interested it is considered good practice to re-use the input message rather than creating a new message. That allows users to add data properties (eg msg.mydata)to a message and pass it through nodes without losing the extra information. Also it usually saves a bit of code. So in your case you could replace the end section with

msg.payload = actype + " " + reg + " Overflew @ " + time +" Altitude " + alt +" HDG " + hdg + " Sqk " + squawk;
return msg

You don't need to provide an array to the return statement unless you are sending to multiple outputs.

Colin, thank you.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.