draftbit / twitter-lite

A tiny, full-featured, flexible client / server library for the Twitter API
https://npm.im/twitter-lite
MIT License
794 stars 95 forks source link

TypeError: Cannot read property 'screen_name' of undefined #109

Open kieron opened 4 years ago

kieron commented 4 years ago

Hey, sorry just got this and wondered if anyone knows what it means, is it saying that the account doesn't have a screen_name? I thought that would be impossible!

events.js:180
      throw er; // Unhandled 'error' event
      ^
TypeError: Cannot read property 'screen_name' of undefined
    at getName (/usr/src/app/app.js:163:20)
    at e.<anonymous> (/usr/src/app/app.js:65:25)
    at emit (events.js:203:13)
    at e.parse (/usr/src/app/node_modules/twitter-lite/dist/stream.js:1:386)
    at Gunzip.<anonymous> (/usr/src/app/node_modules/twitter-lite/dist/twitter.js:1:4461)
    at Gunzip.emit (events.js:203:13)
    at addChunk (_stream_readable.js:294:12)
    at readableAddChunk (_stream_readable.js:275:11)
    at Gunzip.Readable.push (_stream_readable.js:210:10)
    at Gunzip.Transform.push (_stream_transform.js:150:32)
Emitted 'error' event at:
    at e.parse (/usr/src/app/node_modules/twitter-lite/dist/stream.js:1:435)
    at Gunzip.<anonymous> (/usr/src/app/node_modules/twitter-lite/dist/twitter.js:1:4461)
    [... lines matching original stack trace ...]
    at Gunzip.Transform.push (_stream_transform.js:150:32)
    at Zlib.processCallback (zlib.js:516:10) {
  source: { delete: { status: [Object], timestamp_ms: '1591882565122' } }
}

It doesn't happen all the time, just occasionally.

Many thanks!

dandv commented 4 years ago

That means the returned object was undefined, and undefined.screen_name is not a valid property in JavaScript.

What was your code that fetched that object?

kieron commented 4 years ago

Oh! I've been doing this to try and stop the issue, but perhaps a pointless effort, (stripped out a lot of unrelated stuff)

stream.on("data", function(tweet) {
    tweetObj = {
            screenName: getName(tweet),
    }
    // do stuff with tweetObj (which would have, text, time, etc (all tweet components))
});

function getName(tweet) {
    if (tweet.user.hasOwnProperty("screen_name")) {
        return tweet.user.screen_name
    }
    else if (tweet.user.hasOwnProperty("name")) {
        return tweet.user.name
    } 
    else {
        return "Name Null"
    }
}