0

I have this code snippet from the twit module of nodejs:

 function tweets() {
        T.get('search/tweets', { q: input, since:'2013-11-11', count: 3 }, function(err, data, response) {
            if (data) {
                  sendMain(data.statuses[0].text);
            }
            else if (err) {
                sendMain(err);
            }
})
    }

and everything works fine except the error handling, for example, if I search for food, a tweet will pop up, but if I search for ljfhlsjkdfhsldf if comes with the error of:

              sendMain(data.statuses[0].text);
                                       ^
TypeError: Cannot read property 'text' of undefined
    at c:\Users\karim_000\dropbox\main\bot2.js:235:44
    at responseHandler (c:\Users\karim_000\dropbox\main\node_modules\twit\lib\oa
request.js:375:12)
    at passBackControl (c:\Users\karim_000\dropbox\main\node_modules\twit\node_m
odules\oauth\lib\oauth.js:367:11)
    at IncomingMessage.<anonymous> (c:\Users\karim_000\dropbox\main\node_modules
\twit\node_modules\oauth\lib\oauth.js:386:9)
    at IncomingMessage.emit (events.js:117:20)
    at _stream_readable.js:943:16
        at process._tickCallback (node.js:419:13)
    Program node bot2 exited with code 8

Why isnt my error handling working? any help? Thanks.

3
  • data.statuses exists, but it's an empty array. Commented Dec 7, 2014 at 16:03
  • @FlorianMargaine So How can I do some sort of error handling if the array is empty? Commented Dec 7, 2014 at 17:10
  • if (!data.statuses.length) error() Commented Dec 7, 2014 at 17:11

1 Answer 1

2

This isn't necessarily an error that is occuring. You are getting the data parameter back, however, my guess is that data is either undefined or null.

Have you tried to console.log('data: ' + data); to see what is returning? That will tell you for sure what is returning back in that data parameter.

Sign up to request clarification or add additional context in comments.

2 Comments

data is undefined, and what I tried to console.log what you said, it returned: [object Object]
Data is shown but when its undefined, nothing shows up as the program exists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.