# Why does this function not work?

**URL:** <https://discourse.nodered.org/t/why-does-this-function-not-work/42202>\
**Category:** General\
**Created:** [7 March 2021 15:06 UTC](https://discourse.nodered.org/t/why-does-this-function-not-work/42202 "2021-03-07T15:06:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sfedo](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@sfedo](https://discourse.nodered.org/u/sfedo)\
**Post date:** [7 March 2021 15:06 UTC](https://discourse.nodered.org/t/why-does-this-function-not-work/42202/1 "2021-03-07T15:06:19Z")

</div>

I try to achieve following function:

```auto
if (msg.payload=="double_clicked"){
 return [{payload:msg.payload.on=true},
        {payload:msg.payload.brightness=20}
        ]
     
}

```

What am I missing? Why I cant set and return an object?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [7 March 2021 15:43 UTC](https://discourse.nodered.org/t/why-does-this-function-not-work/42202/2 "2021-03-07T15:43:28Z")

</div>

Assignment should be done outside of the object creation.

Also, if `msg.payload` **REALLY DOES** == `"double_clicked"`, then it is a `string` (not an `object`) so you can't suddenly set `msg.payload.on=true` anyway!

I assume this is what you want...

```auto
if (msg.payload=="double_clicked"){
    msg.payload = {  
        on: true, 
        brightness: 20 
    } 
    return msg;
}

```

... but its hard to tell. Maybe if you explain what you _ **need** _

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [7 March 2021 15:46 UTC](https://discourse.nodered.org/t/why-does-this-function-not-work/42202/3 "2021-03-07T15:46:52Z")

</div>

If you are trying to return an array of messages (one to each output) then you want, inside the `if`  
`return [{payload: {on: true}}, {payload: {brightness: 20})]`  
but, as Steve says, we are just guessing.

---

<div class="post-metadata">

**Author:** ![sfedo](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@sfedo](https://discourse.nodered.org/u/sfedo)\
**Post date:** [7 March 2021 16:20 UTC](https://discourse.nodered.org/t/why-does-this-function-not-work/42202/4 "2021-03-07T16:20:51Z")

</div>

exactly what I was looking for, thank you!  
and yes it is possible to compare the string with if statement.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [21 March 2021 16:21 UTC](https://discourse.nodered.org/t/why-does-this-function-not-work/42202/5 "2021-03-21T16:21:50Z")

</div>

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