Promise giving "Cannot read property 'base' of undefined"
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.
Same issue here
Seems like a pretty simple bug...I made a PR https://github.com/desmondmorris/node-twitter/pull/270
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 thanks .you just saved me .it did solve the issue for me .
@HarryEMartland can you explain why your solution worked?
@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.