const User = require('../models/User'); const jwt = require('jsonwebtoken'); const异步= require('./ async'); const ErrorHandler = require('..utils / errorHandler');
exports.protect =异步(异步(res,req,next)=> {if(req.headers.authorization && req.headers.authorization.startsWith('Bearer'))){让令牌= req.headers.authorization .split('')[1]}
if(!token){返回下一个(new ErrorHandler('Unauthorized,401'))}尝试{const tokenVerify = jwt.verify(token,process.env.SECRET)req.user = await User.findById(decoded .id);
next(); } catch(err){返回下一个(new ErrorHandler('Unauthorized,401'))}))
你好,好人,你能告诉我为什么我每次在邮递员中执行以上代码时,都会得到typeError:无法读取未定义的属性“ authorization”吗? 我需要在所有必要的路由器中进行保护
答案 0 :(得分:1)
很简单,您需要反转req和res;)
req
在原型中的res
之前
答案 1 :(得分:0)
顺序将是(req,res)而不是(rea,req)。