node-twitter icon indicating copy to clipboard operation
node-twitter copied to clipboard

Promise giving "Cannot read property 'base' of undefined"

Open nelsyeung opened this issue 8 years ago • 6 comments

Use node 8.9.1

npm init
npm i -S twitter

Create an index.js file with:

const Twitter = require('twitter');
const twitter = new Twitter({
    consumer_key: 'x',
    consumer_secret: 'x',
    access_token_key: 'x',
    access_token_secret: 'x',
});

twitter.get('favorites/list').then((tweet) => { console.log(tweet); });

Run the script

node index.js

Error:

/Users/nelsyeung/Sites/test/node_modules/twitter/lib/twitter.js:119
  if (typeof params.base !== 'undefined') {
                    ^

TypeError: Cannot read property 'base' of undefined
    at Twitter.__request (/Users/nelsyeung/Sites/test/node_modules/twitter/lib/twitter.js:119:21)
    at Twitter.get (/Users/nelsyeung/Sites/test/node_modules/twitter/lib/twitter.js:236:15)
    at Object.<anonymous> (/Users/nelsyeung/Sites/test/index.js:10:9)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)
    at startup (bootstrap_node.js:187:16)

This error does not appear when running with a callback.

nelsyeung avatar Nov 22 '17 20:11 nelsyeung

Same issue here

ryangiglio avatar Dec 03 '17 04:12 ryangiglio

Seems like a pretty simple bug...I made a PR https://github.com/desmondmorris/node-twitter/pull/270

ryangiglio avatar Dec 03 '17 05:12 ryangiglio

If you add an empty config object you can get round this issue e.g. client.get('favorites/list', {}).then((tweet) => { console.log(tweet); });

HarryEMartland avatar Jan 20 '18 17:01 HarryEMartland

@HarryEMartland thanks .you just saved me .it did solve the issue for me .

k3nnet avatar Mar 28 '18 23:03 k3nnet

@HarryEMartland can you explain why your solution worked?

neer17 avatar Sep 03 '18 07:09 neer17

@neer17 If you look at the documentation on requests, you'll see that it expects a params parameter for the function call: client.get(path, params, callback);. If we omit this params parameter, it'll fail on the following block of code within node-twitter, because params doesn't exists thus cannot access params.base:

if (typeof params.base !== 'undefined') {
  base = params.base;
  delete params.base;
}

Calling client.get with an empty config object {}, e.g., client.get(path, {}, callback); means that we are getting around the problem that params doesn't exist by setting it to {}. I hope this is clear enough.

nelsyeung avatar Sep 03 '18 09:09 nelsyeung