1

I'm trying to do a simple condition check of a property with the following:

 if ( message.updates[0].message != 'undefined' && message.updates[0].message.message != 'undefined') {
                                                                                     ^

TypeError: Cannot read property 'message' of undefined

I also tried:

 if (typeof message.updates[0].message.message != 'undefined') 

 if (message.updates[0].message.message.length > 0)

 if (typeof message.updates[0].message.message != undefined) 

 if ( message.updates[0].message != 'undefined' && message.updates[0].message.message != 'undefined') {
 

None work, I'm getting the same error everytime.

here is the data I'm doing the check on as requested in the answers.

for the below data, it should not enter the IF and simply pass it, not give error of undefined.

Thanky you!

{
  _: 'updates',
  updates: [
    {
      _: 'updateReadChannelInbox',
      flags: 0,
      channel_id: 1415629976,
      max_id: 46,
      still_unread_count: 0,
      pts: 48
    },
    {
      _: 'updateNewChannelMessage',
      message: [Object],
      pts: 48,
      pts_count: 1
    }
  ],
  users: [
    {
      _: 'user',
      flags: 33561715,
      self: true,
      contact: true,
      mutual_contact: true,
      deleted: false,
      bot: false,
      bot_chat_history: false,
      bot_nochats: false,
      verified: false,
      restricted: false,
      min: false,
      bot_inline_geo: false,
      support: false,
      scam: false,
      apply_min_photo: true,
      first_name: 'Andrei',
      phone: '40756274034',
      photo: [Object],
      status: [Object]
    }
  ],
  chats: [
    {
      _: 'channel',
      flags: 286977,
      creator: true,
      left: false,
      broadcast: false,
      verified: false,
      megagroup: true,
      restricted: false,
      signatures: false,
      min: false,
      scam: false,
      has_link: false,
      has_geo: false,
      slowmode_enabled: false,
      call_active: false,
      title: 'tester',
      photo: [Object],
      date: 1616253844,
      version: 0,
      admin_rights: [Object],
      default_banned_rights: [Object]
    }
  ],
  date: 1616336995,
  seq: 0
}

johnnasx
  • 158
  • 10
  • You're missing a no-quotes check for `message.updates[0].message`, and in any case, maybe start with checking parents first and work your way up? – Kalnode Mar 21 '21 at 14:29

2 Answers2

1

Change it to be:

if (message.updates[0]?.message?.message !== undefined) {

This is the optional chaining operator and will automatically handle the undefined errors for you.

Dylan Kerler
  • 2,007
  • 1
  • 8
  • 23
0

Your first statement is currently:

if ( message.updates[0].message != 'undefined' && message.updates[0].message.message != 'undefined') {}

You've got undefined as a string in your expression. But undefined is a primitive, not a string, so shouldn't be wrapped in quotes. (However, a typeof assertion returns a string, because the type of an undefined is 'undefined'.)

Also, check out this post which goes deeper on the question of detecting undefined properties.

XML
  • 19,206
  • 9
  • 64
  • 65