tomas / needle

Nimble, streamable HTTP client for Node.js. With proxy, iconv, cookie, deflate & multipart support.

Home Page:https://www.npmjs.com/package/needle

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeError: callback is not a function

dhowe opened this issue · comments

Strange error on OSX (catalina) using the twitter api example [here] (https://github.com/twitterdev/Twitter-API-v2-sample-code/blob/master/Filtered-Stream/filtered_stream.js) The first time I run the script, things work as expected. After quitting (via Ctrl-C) and restarting, I get the error below. After waiting a few minutes, it works again. I'm guessing the port (or some resource) is not being fully closed on exit ?

~/git/ecobot/node_modules/needle/lib/needle.js:460
      return callback(err, resp, resp ? resp.body : undefined);
             ^

TypeError: callback is not a function
    at done (~/git/ecobot/node_modules/needle/lib/needle.js:460:14)
    at PassThrough.<anonymous> (/~/git/ecobot/node_modules/needle/lib/needle.js:711:9)
    at PassThrough.emit (events.js:321:20)
    at endReadableNT (_stream_readable.js:1220:12)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)

I can confirm that it's not limited to OSX as I tried the same example with Node 14 on Debian, with and without Docker image.

Any solution to this ?

https://github.com/twitterdev/Twitter-API-v2-sample-code/blob/master/Filtered-Stream/filtered_stream.js#L85-L89 is running

needle.get(streamURL, {
    headers: { 
        Authorization: `Bearer ${token}`
    }
}, {
    timeout: 20000
});

According to https://github.com/tomas/needle#request-options, both headers and timeout go in the same options object. When an options object is passed to needle.get, the third argument is expected to be a function (see needle.get(url[, options][, callback]) on https://github.com/tomas/needle#needlegeturl-options-callback).

I used to see this error every couple days, changed it to the following two weeks ago, and haven't seen it since:

needle.get(streamURL, {
    headers: { 
        Authorization: `Bearer ${token}`
    },
    timeout: 20000
});

Opened xdevplatform/Twitter-API-v2-sample-code#8 to add that change.