{
"message": "Cannot read property '_doc' of undefined",
"error": {}
}
以下是相关的IT技术内容:
var User = require('../models/user');
var jwt = require('jsonwebtoken');
var config = require('../config.js');
exports.getToken = function (user) {
return jwt.sign(user, config.secretKey, {
expiresIn: 3600
});
};
exports.verifyOrdinaryUser = function (req, res, next) {
// check header or url parameters or post parameters for token
var token = req.body.token || req.query.token || req.headers['x-access-token'];
// decode token
if (token) {
// verifies secret and checks exp
jwt.verify(token, config.secretKey, function (err, decoded) {
if (err) {
var err = new Error('You are not authenticated!');
err.status = 401;
return next(err);
} else {
// if everything is good, save to request for use in other routes
req.decoded = decoded;
next();
}
});
} else {
// if there is no token
// return an error
var err = new Error('No token provided!');
err.status = 403;
return next(err);
}
};
exports.verifyAdmin = function(req,res,next){
if(req.decoded._doc.admin !== true) {
return next(err);
}else {
return next();
}
};
我确定我在verifyAdmin函数中弄错了什么。 中间件顺序看起来没问题 欢迎提出建议
谢谢
编辑:中间件在app.js中
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
// passport config
var User = require('./models/user');
app.use(passport.initialize());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
app.use('/dishes',dishRouter);
app.use('/promotions',promoRouter);
app.use('/leadership',leaderRouter);
app.use
的那几行代码吗?我知道你说看起来没问题,但我这里看不出有什么错。 - rtfapp.use
。:) 你已经导出了3个可以用作中间件的函数。在某个时候,你应该会执行app.use(foo.verifyOrdinaryUser);
然后是app.use(foo.verifyAdmin);
,对吧? - rtf