Schmavery / facebook-chat-api

Unofficial Facebook Chat API for Nodejs
MIT License
1.93k stars 596 forks source link

Cannot read property 'name' of undefined in getThreadInfo #260

Closed drkno closed 8 years ago

drkno commented 8 years ago

On one chat (one only, all others work fine) when I call api.getThreadInfo, this error is thrown:

ERR! Error in getThreadInfo TypeError: Cannot read property 'name' of undefined
ERR! Error in getThreadInfo     at /Users/matthew/Documents/Projects/Kassy/node_modules/facebook-chat-api/src/getThreadInfo.js:36:46
ERR! Error in getThreadInfo     at tryCatcher (/Users/matthew/Documents/Projects/Kassy/node_modules/bluebird/js/main/util.js:26:23)
ERR! Error in getThreadInfo     at Promise._settlePromiseFromHandler (/Users/matthew/Documents/Projects/Kassy/node_modules/bluebird/js/main/promise.js:507:31)
ERR! Error in getThreadInfo     at Promise._settlePromiseAt (/Users/matthew/Documents/Projects/Kassy/node_modules/bluebird/js/main/promise.js:581:18)
ERR! Error in getThreadInfo     at Promise._settlePromises (/Users/matthew/Documents/Projects/Kassy/node_modules/bluebird/js/main/promise.js:697:14)
ERR! Error in getThreadInfo     at Async._drainQueue (/Users/matthew/Documents/Projects/Kassy/node_modules/bluebird/js/main/async.js:123:16)
ERR! Error in getThreadInfo     at Async._drainQueues (/Users/matthew/Documents/Projects/Kassy/node_modules/bluebird/js/main/async.js:133:10)
ERR! Error in getThreadInfo     at Immediate.Async.drainQueues [as _onImmediate] (/Users/matthew/Documents/Projects/Kassy/node_modules/bluebird/js/main/async.js:15:14)
ERR! Error in getThreadInfo     at tryOnImmediate (timers.js:534:15)
ERR! Error in getThreadInfo     at processImmediate [as _immediateCallback] (timers.js:514:5)
ERR! Error in getThreadInfo  [TypeError: Cannot read property 'name' of undefined]

I logged the contents of both threadData and userData before that line:

threadData

{ thread_id: 'id.874047089376806',
  thread_fbid: '874047089376806',
  other_user_fbid: null,
  participants:
   [ 'fbid:100000645113290',
     'fbid:100009621438986',
     'fbid:100001395408146' ],
  former_participants: [],
  name: '',
  snippet: '/ping',
  snippet_has_attachment: false,
  snippet_attachments: [],
  snippet_sender: 'fbid:100000645113290',
  unread_count: 30,
  message_count: 30,
  image_src: null,
  timestamp_absolute: 'Today',
  timestamp_datetime: '10:49',
  timestamp_relative: '10:49',
  timestamp_time_passed: 0,
  timestamp: 1461192561393,
  server_timestamp: 1461192561393,
  mute_settings: [],
  is_canonical_user: false,
  is_canonical: false,
  is_subscribed: true,
  folder: 'inbox',
  is_archived: false,
  recipients_loadable: true,
  name_conversation_sheet_dismissed: false,
  has_email_participant: false,
  read_only: false,
  can_reply: true,
  composer_enabled: true,
  last_message_timestamp: 1461192561393,
  last_read_timestamp: -1,
  last_message_type: 'non_ad',
  ephemeral_ttl_mode: 0,
  custom_like_icon: null,
  titan_originated_thread_id: '<1447464217549:0-62d7f9d8842e88f1@mail.projektitan.com>',
  custom_nickname: null,
  custom_color: null,
  admin_ids: [],
  customization_enabled: true }

userData

undefined

The code that is calling the method is simply passing the event.threadID property:

api.getThreadInfo(event.threadID, function(err, info) {
     ...
});

(The rest of the code is here).

The version of the API that is being used is 1.1.0.

Schmavery commented 8 years ago

Ahh... Good old js falsy values... Empty string is counting as false and so userData.name is getting evaluated.. We can probably change that null coalescing operator into some inline conditional like threadData.name != null ? threadData.name : userData.name or any similar variation.

I expect this will happen in any group chat that is not explicitly named.

alexisohayon commented 8 years ago

Same issue here :) Do you plan to release a fix soon?

Schmavery commented 8 years ago

I'm out of town. @bsansouci Ben can you fix this?

demurgos commented 8 years ago

This issue is not completely solved: the tests still expect the name to be a non-empty string but "" is a legal value.

// current:
assert(info.name != null && info.name.length > 0);
// fixed:
assert(info.name != null);

https://github.com/Schmavery/facebook-chat-api/blob/4f9b55911fc428606cff3ccfa52b21794ecd9820/test/test.js#L111

bsansouci commented 8 years ago

Oh good catch!