How to check if an Reference in msg.payload exists?

Sometimes i get following reference in msg.payload and sometimes not.


How can i check in a function if the reference 'Test' in payload exists or not?

payload always exists but sometimes with 'Test' and sometimes without.

If not exists i get following Error.
ReferenceError: references is not defined

I tried:
"if (msg.payload.hasOwnProperty("Test")){do something}"

But i don't know if i used it correctly or it is the right commando.

Anybody an idea?
Thanks in advise.

There are a few ways to do it, and the one you've identified, the hasOwnProperty function, is certainly a way to be sure it exists.

if (msg.payload.hasOwnProperty("Test")){
  // do something

One thing to be careful of is this check only tests if there is a property called Test. The value of the property could still be null or otherwise blank. So depending on your scenario, you may want to also check the value of Test before using it.

Thank's for your quick help.
It works fine for me.