Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript TypeError:无法读取属性';州';未定义的 错误 要求_Javascript_Node.js_Oauth_Reddit - Fatal编程技术网

Javascript TypeError:无法读取属性';州';未定义的 错误 要求

Javascript TypeError:无法读取属性';州';未定义的 错误 要求,javascript,node.js,oauth,reddit,Javascript,Node.js,Oauth,Reddit,console.log('request')的输出是: var RedditApi = require('reddit-oauth'); var request = require('request'); var reddit = new RedditApi({ app_id: **********, app_secret: ********* , redirect_uri: 'http://localhost:8888' }); // Authenticate wi

console.log('request')
的输出是:

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);
    }
);
注意:已编辑代码以反映以下注释



不确定为什么这段代码会出现上述错误

我很确定这是无效的:

{ [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] }
这就像执行
const request.query='asdf'

请改为尝试(从请求中解构查询):


该错误意味着
query
undefined
@Pointy应该如何定义
query
您是否调试并查看了请求对象的状态?我敢打赌请求对象只是没有定义查询属性。换句话说,
request.query===undefined
@tinkertenorsoftware,此时为止的代码已在问题上更新。在调用
reddit.oAuthTokens
之前,请尝试
console.log(request)
。请求对象中有什么?您是否看到
query
了?
if(query.state!==state | |!query.code){
它指向query.state,表示没有定义查询,所以在文件的某个地方,您引用了
query
,但没有定义它,这意味着需要像
var query='something'
这样定义它,或者需要将它传递到
函数(something,query){…}这样的函数中
That或
query.state
未定义,但
query.code
是。您缺少的是找出代码执行
if(query.state!==state | |!query.code)的原因{
在创建或添加
query.state
之前。我无法进一步帮助您,除非您向我展示
query
的来源。我在您的文件中没有看到它,这可能与您的问题有关,因为它说“谁是查询者”当计算
query.state
是否不等于
state
时。您的代码当前正在将其传递到
reddit.oAuthTokens()
,但查询从何而来。
{ [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] }
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);
    }
);
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);
    }
);