Search code examples
javascriptnode.jsoauthreddit

TypeError: Cannot read property 'state' of undefined


Error

/Users/sharanduggirala/Documents/UID/CS235ProjectJS/node_modules/reddit-oauth/lib/index.js:[email protected]~preinstall: [email protected]
        if (query.state !== state || !query.code) {
                 ^

TypeError: Cannot read property 'state' of undefined
    at RedditApi__oAuthTokens [as oAuthTokens] (/Users/sharanduggirala/Documents/UID/CS235ProjectJS/node_modules/reddit-oauth/lib/index.js:278:18)
    at Object.<anonymous> (/Users/sharanduggirala/Documents/UID/CS235ProjectJS/buildScripts/srcReddit.js:30:8)
    at Module._compile (module.js:624:30)
    at loader (/Users/sharanduggirala/Documents/UID/CS235ProjectJS/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/sharanduggirala/Documents/UID/CS235ProjectJS/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:545:32)
    at tryModuleLoad (module.js:508:12)
    at Function.Module._load (module.js:500:3)
    at Function.Module.runMain (module.js:665:10)
    at /Users/sharanduggirala/Documents/UID/CS235ProjectJS/node_modules/babel-cli/lib/_babel-node.js:159:24

Code

I am currently trying to use the Reddit API, and am trying to get the example on this page to work. This is the error that I got when trying to compile the code below:

var RedditApi = require('reddit-oauth');
var request = require('request');

var reddit = new RedditApi({
    app_id: **********,
    app_secret: ********* ,
    redirect_uri: 'http://localhost:8888'
});

// Authenticate with username/password
reddit.passAuth(
    'sharan100',
    '********',
    function (success) {
        if (success) {
            // Print the access token we just retrieved
            console.log(reddit.access_token);
        }
    }
);

// Get the OAuth URL to redirect users to
// Scopes are defined here: https://github.com/reddit/reddit/wiki/OAuth2
reddit.oAuthUrl('some_state', 'identity');

// After the user is redirected back to us, grab the query string
// object and exchange it for a set of access and refresh tokens.
// Scope has to be identical as the one provided to oAuthUrl. Can
// change for each authentication attempt.
reddit.oAuthTokens(
    'some_state',
    request.query,
    function (success) {
        // Print the access and refresh tokens we just retrieved
        console.log(reddit.access_token);
        console.log(reddit.refresh_token);
    }
);

Request

The output of console.log('request') is:

{ [Function: request]
  get: [Function],
  head: [Function],
  options: [Function],
  post: [Function],
  put: [Function],
  patch: [Function],
  del: [Function],
  delete: [Function],
  jar: [Function],
  cookie: [Function],
  defaults: [Function],
  forever: [Function],
  Request:
   { [Function: Request]
     super_:
      { [Function: Stream]
        super_: [Object],
        Readable: [Object],
        Writable: [Object],
        Duplex: [Object],
        Transform: [Object],
        PassThrough: [Object],
        Stream: [Circular],
        _isUint8Array: [Function: isUint8Array],
        _uint8ArrayToBuffer: [Function: _uint8ArrayToBuffer] },
     debug: undefined,
     defaultProxyHeaderWhiteList:
      [ 'accept',
        'accept-charset',
        'accept-encoding',
        'accept-language',
        'accept-ranges',
        'cache-control',
        'content-encoding',
        'content-language',
        'content-location',
        'content-md5',
        'content-range',
        'content-type',
        'connection',
        'date',
        'expect',
        'max-forwards',
        'pragma',
        'referer',
        'te',
        'user-agent',
        'via' ],
     defaultProxyHeaderExclusiveList: [ 'proxy-authorization' ] },
  initParams: [Function: initParams],
  debug: [Getter/Setter] }

Note: Code has been edited to reflect the comments below.


Not sure why this code seems to be getting the errors above.


Solution

  • I'm pretty sure this isn't valid:

    reddit.oAuthTokens(
        'some_state',
        // this
        request.query,
        function (success) {
            // Print the access and refresh tokens we just retrieved
            console.log(reddit.access_token);
            console.log(reddit.refresh_token);
        }
    );
    

    it's like doing const request.query = 'asdf'

    try instead (destructure query off request):

    const { query } = request
    // or: var query = request.query
    
    reddit.oAuthTokens(
        'some_state',
        // this
        query,
        function (success) {
            // Print the access and refresh tokens we just retrieved
            console.log(reddit.access_token);
            console.log(reddit.refresh_token);
        }
    );