"TypeError: Cannot create property '_msgid' on string

#1

Sorry but I am very new to Node Red and struggling a bit. I got the above error on a function node trying to store an IP address to a variable. I am using a text node to capture the IP address from the user on my dashboard.
Here is the code in the function node. Any help is greatly appreciated.
var address = msg.payload;
return address;

0 Likes

#2

Hi @mikemassey1968

Your code is returning just current value of msg.payload, which is presumably is a String.

Function nodes need to return message objects, not individual values. By convention, nodes will use msg.payload - so if that's where you already have the IP address, that's probably the right place for it to be. The question is what you want to do with it next.

Worth reading https://nodered.org/docs/writing-functions if you haven't already.

Nick

0 Likes

#3

That is where it gets even harder for me. i am trying to supplement a variable (flow level) into a json payload in place of hard coding the IP address. The dashboard I am writing talks to an industrial controller and the dashboard could be used at many different locations so the IP address of the device needs to be updated after installation.
here is an example of the json payload and where I would like to use the variable in place of a hard coded IP address:
{"tlps":[[66,0,37, 1]], "host":"10.0.0.2", "port":4000, "deviceAddress":240, "deviceGroup":240}
The payload above works but what I would like to do is make this more dynamic like:
{"tlps":[[66,0,37, 1]], "host":"address", "port":4000, "deviceAddress":240, "deviceGroup":240}

0 Likes

#4

Probably also worth reading https://nodered.org/docs/user-guide/messages

0 Likes

#5

Thank you I will look at it again. Also the link form Nick above. Something is not sinking in for me but a reread never hurts.

0 Likes

#6

What I have not mentioned is the need to store the IP Address in the variable "address" at the flow level. I understand I should be able to do this with the following code
flow.set("address":msg.payload);
and I should be able to retrieve it using
var IPaddress = flow.get("address");
but retrieving it does not work as part of an input node based on my understand and searches. This capability (dynamic variables as part of a json payload) appears to be an enhancement on the json trello board for future consideration

There is likely another way to skin this cat but being new I have not found it or don't yet know where I should be looking

Mike

0 Likes

#7

Hi @mikemassey1968

to set a flow context variable in a Function node, you would use:

flow.set("address", msg.payload);

Note the , rather than : that you have in your example (I'll assume that's just a typo in your post).
Or you could use a Change node to do it - configured to:

Set flow.address to msg.payload

You can then retrieve that in another node on the same tab with:

var IPaddress = flow.get("address");

If you want to be able to get it in a node on a different tab, you'd have to use global.set and global.get for global context that spans all tabs.

Assuming you do successfully get it stored in flow context, then a Function to create the full object you want would be:

var IPaddress = flow.get("address");
msg.payload = {
   "tlps": [[66,0,37,1]],
   "host": IPaddress,
   "port":4000,
   "deviceAddress":240,
   "deviceGroup":240
};
return msg;
0 Likes

#8

Yes the : was a typo sorry about that.
So I would need to handle these changes in a function vs using an input node like "button" and being able to edit the json payload since I cannot perform the statement:
var IPaddress = flow.get("address");
in a json payload correct?
I cannot perform a
flow.set("address", msg.payload);
statement in a function and the use the following as the json payload in a "button":
{"tlps":[[66,0,37, 1]], "host":address, "port":4000, "deviceAddress":240, "deviceGroup":240}
I appreciate the guidance and the quick replies!! let me see how it goes with this path.

0 Likes

#9

I got this to work and found another issue. I was using flow but needed to use global context this function was part of a subflow. I assumed a subflow was part of a flow but that is not the case. In any case I got it working! thank you again for the help.

0 Likes