无法读取属性' access_token"未定义的

时间:2017-07-24 11:47:26

标签: facebook api express

我正在使用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');
      }
    })
}

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

1 个答案:

答案 0 :(得分:0)

您的body请求中的req属性似乎未定义。您应该检查您是否要发送正确的数据。也许您可以使用邮递员或其他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');
      }
    })
}