我想实现请求身份验证,但是始终出现此错误:
错误:参数已传递给 findByPk无效:[object Object] 在Function.findByPk(/home/admin/Desktop/project/node_modules/sequelize/lib/model.js:1714:13) 在Function.Model.findById(/home/admin/Desktop/project/node_modules/sequelize/lib/model.js:4209:25) 在JwtStrategy.passport.use.JwtStrategy上[作为_verify](/home/admin/Desktop/project/middleware/passport.js:16:41) 在/home/admin/Desktop/project/node_modules/passport-jwt/lib/strategy.js:123:34 在/home/admin/Desktop/project/node_modules/jsonwebtoken/verify.js:196:12 在getSecret(/home/admin/Desktop/project/node_modules/jsonwebtoken/verify.js:76:14) 在Object.module.exports [作为验证](/home/admin/Desktop/project/node_modules/jsonwebtoken/verify.js:80:10) 在Function.module.exports [作为JwtVerifier](/home/admin/Desktop/project/node_modules/passport-jwt/lib/verify_jwt.js:4:16)中 在/home/admin/Desktop/project/node_modules/passport-jwt/lib/strategy.js:104:25 在JwtStrategy._secretOrKeyProvider(/home/admin/Desktop/project/node_modules/passport-jwt/lib/strategy.js:40:13) 在JwtStrategy.authenticate(/home/admin/Desktop/project/node_modules/passport-jwt/lib/strategy.js:99:10) 尝试(/home/admin/Desktop/project/node_modules/passport/lib/middleware/authenticate.js:361:16) 在进行身份验证时(/home/admin/Desktop/project/node_modules/passport/lib/middleware/authenticate.js:362:7) 在Layer.handle [作为handle_request](/home/admin/Desktop/project/node_modules/express/lib/router/layer.js:95:5) 在下一个(/home/admin/Desktop/project/node_modules/express/lib/router/route.js:137:13) 在Route.dispatch(/home/admin/Desktop/project/node_modules/express/lib/router/route.js:112:3) 在Layer.handle [作为handle_request](/home/admin/Desktop/project/node_modules/express/lib/router/layer.js:95:5) 在/home/admin/Desktop/project/node_modules/express/lib/router/index.js:281:22 在Function.process_params(/home/admin/Desktop/project/node_modules/express/lib/router/index.js:335:12) 在下一个(/home/admin/Desktop/project/node_modules/express/lib/router/index.js:275:10) 在Function.handle(/home/admin/Desktop/project/node_modules/express/lib/router/index.js:174:3) 在路由器上(/home/admin/Desktop/project/node_modules/express/lib/router/index.js:47:12)
我将PostgreSQL用作数据库,将Sequelize用作ORM。
路线:
const express = require('express')
const router = express.Router()
const controller = require('../controllers/user')
const passport = require('passport')
// localhost:5000/api/admin/users
router.get('/users', passport.authenticate('jwt', {session: false}), controller.getAll)
module.exports = router
护照:
const JwtStrategy = require('passport-jwt').Strategy
const ExtractJwt = require('passport-jwt').ExtractJwt
const db = require('../config/db.config.js')
const User = db.user
const options = {
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: process.env.SECRET_OR_KEY
}
module.exports = passport => {
passport.use(
new JwtStrategy(options, async (payload, done) => {
try {
const user = await User.findById({
where: {
id: payload.id
}
}).select('username id')
if (user) {
done(null, user)
} else {
done(null, false)
}
} catch(e) {
console.log(e)
}
})
)
}
控制器:
const db = require('../config/db.config.js')
const User = db.user
const errorHandler = require('../utils/errorHandler')
module.exports.getAll = async function(req, res) {
try {
const users = await User.findAll({user: req.user.id})
res.status(200).json(users)
} catch(e) {
errorHandler(req, e)
}
}
答案 0 :(得分:0)
在您的护照代码中,您正在呼叫User.findById
并传递where子句。
我认为应该只是User.findById(payload.id)