2016-08-23 2 views
1

に未定義の 'スプリット':TypeError例外:プロパティを読み取ることができませんが、私はJWT-シンプルLIBを使用して、次のエラーを取得Nodejs

var jwt = require('jwt-simple'); 


module.exports = function (req, res) { 
    var token = req.headers.authorization.split(' ')[1]; 
    var payload = jwt.decode(token, "shhh.."); 
    if(!payload.sub) { 
    res.status(401).send({ 
     message: 'Authentication failed' 
    }); 
    } 
    if(!req.headers.authorization){ 
    return res.status(401).send({ 
     message: 'You are not authorized' 
    }); 
    } 
    res.json(mylist); 
}; 

var mylist = [ 
    'Proj 1', 
    'Proj 2', 
    'Proj 3', 
    'Proj 4' 
]; 

TypeError: Cannot read property 'split' of undefined 
    at module.exports (C:\my_application\services\mylist.js:5:40) 
    at Layer.handle [as handle_request] (C:\my_application\node_modules\express\lib\router\layer.js:95:5) 
    at next (C:\my_application\node_modules\express\lib\router\route.js:131:13) 
    at Route.dispatch (C:\my_application\node_modules\express\lib\router\route.js:112:3) 
    at Layer.handle [as handle_request] (C:\my_application\node_modules\express\lib\router\layer.js:95:5) 
    at C:\my_application\node_modules\express\lib\router\index.js:277:22 
    at Function.process_params (C:\my_application\node_modules\express\lib\router\index.js:330:12) 
    at next (C:\my_application\node_modules\express\lib\router\index.js:271:10) 
    at C:\my_application\api.js:39:3 
    at Layer.handle [as handle_request] (C:\my_application\node_modules\express\lib\router\layer.js:95:5) 
    at trim_prefix (C:\my_application\node_modules\express\lib\router\index.js:312:13) 
    at C:\my_application\node_modules\express\lib\router\index.js:280:7 
    at Function.process_params (C:\my_application\node_modules\express\lib\router\index.js:330:12) 
    at next (C:\my_application\node_modules\express\lib\router\index.js:271:10) 
    at logger (C:\my_application\node_modules\morgan\index.js:144:5) 
    at Layer.handle [as handle_request] (C:\my_application\node_modules\express\lib\router\layer.js:95:5) 

、ここではmylist.jsファイルです私は、ユーザーがフロントエンドのmylistリソースにアクセスする権限があるかどうかを調べようとしています。
誰にも分かりませんか?

+1

あなたはここで防衛する必要があります: 'req.headers.authorization.split( '')[1];' –

+0

[未定義オブジェクトプロパティの検出]の可能な複製(http://stackoverflow.com/questions/27509/detect-an-undefined-object-property) –

+0

@ DanielA.White 'split'自体が定義されていないとどうすれば助けになりますか? –

答えて

5

実際に文字列があるかどうかわからなくても、それは文字列とみなします。しかし、なぜ、正確にあなたが第二のトークンをしたいとしません。最初: 最初

module.exports = function (req, res) { 
    if (typeof req.headers.authorization !== 'string') { 
    res.sendStatus(400); 
    return; 
    } 

    var tokens = req.headers.authorization.split(' '); 

    if (tokens.length < 2) { 
    res.sendStatus(400); 
    return; 
    } 

    var token = tokens[1]; 

    var payload = jwt.decode(token, "shhh.."); 
    if(!payload.sub) { 
    res.status(401).send({ 
     message: 'Authentication failed' 
    }); 
    } 
    ... 
}; 

編集]をチェックし、いくつかのエラーを追加する必要がありますか?

+0

それは魅力のように働いています。ありがとう – cplus

+0

私は私のauthinterceptorに 'ベアラー '+トークン'を持っているので私は第二部が必要です。 – cplus

関連する問題