【问题标题】:cannot read property 'access_token" of undefined无法读取未定义的属性“access_token”
【发布时间】:2017-07-24 19:51:49
【问题描述】:

我正在使用 Auth0 并尝试获取 AccessToken。但我遇到了难题。 我怎么解决这个问题? 我遇到了这样的错误:

E:\sample projects\Building-API\movieanalyst-website\server.js:27
      if(req.body.access_token){
                 ^

TypeError: Cannot read property 'access_token' of undefined
    at E:\sample projects\Building-API\movieanalyst-website\server.js:27:18
    at Request.callback (E:\sample projects\Building-API\movieanalyst-website\node_modules\superagent\lib\node\index.js:688:3)
    at E:\sample projects\Building-API\movieanalyst-website\node_modules\superagent\lib\node\index.js:883:18
    at IncomingMessage.<anonymous> (E:\sample projects\Building-API\movieanalyst-website\node_modules\superagent\lib\node\parsers\json.js:16:7)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:188:7)
    at endReadableNT (_stream_readable.js:975:12)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickCallback (internal/process/next_tick.js:104:9)

npm ERR! Windows_NT 10.0.10240
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start"
npm ERR! node v7.9.0
npm ERR! npm  v4.2.0
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! movieanalyst-website@1.0.0 start: `node server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the movieanalyst-website@1.0.0 start script 'node server.js'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the movieanalyst-website package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node server.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs movieanalyst-website
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls movieanalyst-website
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     C:\Users\Assassin\AppData\Roaming\npm-cache\_logs\2017-07-24T11_28_24_478Z-debug.log

根据报错,代码是这样的

function getAccessToken(req, res, next) {
  request
    .post('https://shinji-sakai.auth0.com/oauth/token')
    .send(authData)
    .end(function(err, res) {
      if(req.body.access_token){
        req.access_token = res.body.access_token;
        next();
      } else {
        res.send(401, 'Unauthorized');
      }
    })
}

请给我解决这个问题的方法。 谢谢:)

【问题讨论】:

  • 你研究过错误信息的含义吗?到目前为止,您执行了哪些基本调试步骤?

标签: facebook api express


【解决方案1】:

您的req 请求中的body 属性似乎未定义。您应该检查您的请求,看看您发送的数据是否正确。也许您可以使用 postman 或其他 RESTful API 进行测试。

您也可以更改代码以首先检查body 属性,如下所示(但我认为您的问题出在实际请求上):

function getAccessToken(req, res, next) {
  request
    .post('https://shinji-sakai.auth0.com/oauth/token')
    .send(authData)
    .end(function(err, res) {
      if(req.body && req.body.access_token) {
        req.access_token = res.body.access_token;
        next();
      } else {
        res.send(401, 'Unauthorized');
      }
    })
}

【讨论】:

  • res.send(401, '未授权');然后TypeError: res.send is not a function这是什么?
  • 表示res.send不是函数,所以不能这样调用:res.send(401, 'unauthorized')res 对象也可能是 undefined 或其他不具有 send 函数的类型。检查您是如何调用getAccessToken 函数的以及您向它发送的内容。
  • Shinji,我很想知道你为解决问题做了什么。我有同样的问题,并且一直在尝试并且无法让 auth0 返回 auth_token。邮递员显示请求被拒绝访问。没有授权。
猜你喜欢
  • 2022-12-21
  • 1970-01-01
  • 2020-04-06
  • 2020-10-30
  • 2019-10-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多