# Msg.topic msg.payload syntax

**URL:** <https://discourse.nodered.org/t/msg-topic-msg-payload-syntax/15649>\
**Category:** General\
**Created:** [18 September 2019 00:49 UTC](https://discourse.nodered.org/t/msg-topic-msg-payload-syntax/15649 "2019-09-18T00:49:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [18 September 2019 00:49 UTC](https://discourse.nodered.org/t/msg-topic-msg-payload-syntax/15649/1 "2019-09-18T00:49:09Z")

</div>

Having read all through the forum and having read the node-red docs on functions and such I haven't found a clear answer to my question. This isn't related to a flow problem or a current project but for some reason this came to me and I'm trying to determine the answer.  
So if I put in if (msg.topic == 'test' && msg.payload == 'test') { some answer}  
does this pull from the same message and topic. In other words if there are several messages in the node and one message has the topic of 'test' but the payload is 'none' and another message has the topic of 'next' and a payload of 'test' would the syntax above return a true? In my limited testing over several days it would which is not what I'm looking for. I want the topic and payload from the same message. Can / would someone point me in the right direction please?  
thanks in advance

---

<div class="post-metadata">

**Author:** ![Bobo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobo/32/8401_2.png) [@Bobo](https://discourse.nodered.org/u/Bobo)\
**Post date:** [18 September 2019 02:19 UTC](https://discourse.nodered.org/t/msg-topic-msg-payload-syntax/15649/2 "2019-09-18T02:19:50Z")

</div>

There can only ever be one msg.whatever (topic,payload,xyz etc). The value of "whatever" will be what it was last assigned.

If you need to work on more than one message in a function node then you would have something like msg, msg2, msg3 ...

[https://nodered.org/docs/user-guide/writing-functions](https://nodered.org/docs/user-guide/writing-functions)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [18 September 2019 04:45 UTC](https://discourse.nodered.org/t/msg-topic-msg-payload-syntax/15649/3 "2019-09-18T04:45:09Z")

</div>

> [@gerry](#):
>
> So if I put in if (msg.topic == 'test' && msg.payload == 'test') { some answer}  
> does this pull from the same message and topic.

Though Bobo did answer, I feel I should chip in here too.

Node-red is what I would call "event driven".

So something happens when a message arrives at the input of a node.

Keeping it simple, the "message" has a `payload` and a `topic`.

So when it arrives at the node's input there is only **that** message as far as the node is concerned.

There are ways to allow it to see more, but that is beyond the scope here and now.

So you are correct in this request.

> [@gerry](#):
>
> I want the topic and payload from the same message.

It would be for the one message at that point in time.

Good luck.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [18 September 2019 06:18 UTC](https://discourse.nodered.org/t/msg-topic-msg-payload-syntax/15649/4 "2019-09-18T06:18:48Z")

</div>

> [@](#):
>
> So if I put in if (msg.topic == 'test' && msg.payload == 'test') { some answer}  
> does this pull from the same message and topic.

Yes

> [@](#):
>
> In other words if there are several messages in the node and one message has the topic of 'test' but the payload is 'none' and another message has the topic of 'next' and a payload of 'test' would the syntax above return a true?

Neither message would return true.

The first one does not match because payload is 'none' and the second one does not match because topic is 'next' so your AND fails in both cases
