Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nelsyeung opened this issue Nov 22, 2017 · 6 comments
Open

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

nelsyeung opened this issue Nov 22, 2017 · 6 comments

Comments

@nelsyeung
Copy link

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.

@ryangiglio
Copy link

Same issue here

@ryangiglio
Copy link

Seems like a pretty simple bug...I made a PR #270

@HarryEMartland
Copy link

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

@k3nnet
Copy link

k3nnet commented Mar 28, 2018

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

@neer17
Copy link

neer17 commented Sep 3, 2018

@HarryEMartland can you explain why your solution worked?

@nelsyeung
Copy link
Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants