How to create msg keys?

Hi !
I have some trouble creating "sub-keys" in msg.payload.
This is for telegram bot, I need:
msg.payload.content
msg.payload.chatId
msg.payload.type

I want to build the message in a function node.

Sometime using this is good enough:
msg.payload.type = "message";

But sometime is just says "msg.payload.type is empty" in debug window.

I found a workaround by creating the message this way:
msg = { payload: { chatId : 11111111111 , type : "message" , content : "Hello World" } };

(1111111111 is a dummy value)

But it's a bit "heavy" (in my opinion) sometime and I would prefer set the keys one by one, when I need.

The point is that I don't understand exactly what's the issue, so if I missed something simple, I would be happy to learn :slight_smile:
If there's a better way to create sub-key of msg or msg.payload, I would be happy to know too :slight_smile:

Set msg.payload to a new object before adding properties...

msg.payload = {};
msg.payload.type = 'whatever';
1 Like

Wow, seems to work perfectly !

is it ok to add as many keys I need? I mean:

msg.payload = {};
msg.payload.type = 'whatever';
msg.payload.content ='anythingelse';
msg.payload.somestuff = 123;
...

Yes - you are spot on with that. It also makes it easy to understand visually what's going on.

You can also include in your object...

msg.payload.chatId =123456789;

1 Like

nice! I will update my flows this way.

It rise me a question, can I add a key to msg without losing other keys ?

In the same way msg.payload = {} initialise a new payload, I assume msg = {}; would initialise a new msg (empty).

But say I want to keep msg.payload, msg.topic, whatever, as they are....but I wish to add a key msg.limit ?

maybe simple by using ?
msg.limit = {};

Yes if the payload is already an object.

You can test the payload is an object (and create an object if it is not)

if( typeof msg.payload !== "object" ) {
    msg.payload = {};
}
msg.payload.type = 'whatever';
msg.payload.content ='anythingelse';
msg.payload.somestuff = 123;
1 Like

That's clarify the reason why my initial attempt to simply add msg.payload.type = ... (without msg.payload = {}; ) sometimes worked, sometimes not.
I assume it depends if the payload was already an object or not.
I think it's now very clear on how to manage msg.payload, thank you! :slight_smile:

But I'm still not sure to understand how to add a key to the root msg without loosing other keys.
For example, how to properly add a non existing msg.limit to msg, without effect on msg.payload

msg is always an object. If yo want to add limit then...

msg.limit = {}; 
msg.limit.whatever = 123;

And just like the payload, you can test limit to see if it an object already.

This will not affect msg.payload. Just DONT set msg and all of its properties will remain intact.

Sky is the limit.

1 Like

This :point_up: is what causes msg and all its existing properties to be lost.

1 Like

ah ok, this rewrite the whole msg.
I could use maybe:

msg.payload = {};
msg.payload.chatId : 11111111111;
msg.payload.type : "message";
msg.payload.content : "Hello World";

or directly:

msg.payload = {};
msg.payload = { chatId : 11111111111 , type : "message" , content : "Hello World" };
1 Like

If setting directly, no need to create object...

msg.payload = { chatId : 11111111111 , type : "message" , content : "Hello World" };

:point_up: That's is all that's required.

1 Like

ah gosh, I see, in this case it both create and fill the object in a single line :slight_smile:

1 Like