我正在使用 node.js/express/sequelize 登录用户。我正在测试在 postman 中发出请求,看看它是否得到任何东西。我正在测试我知道在数据库中的用户和密码,但即使测试的名称在数据库中,我在 sequelize 中的 findOne 查询也返回 null。这是有问题的查询:
let { userId, pw } = req.body
try{
const foundUser = await User.findOne({ where: { userId: userId } });
if (foundUser === null) {
return res.status(401).send({ auth: false, token: null,error: 'name not found' });
} else {
const passwordIsValid = bcrypt.compareSync(pw, foundUser.pw);
if (!passwordIsValid) return res.status(401).send({ auth: false, token: null,error: 'name not found' });
const token = jwt.sign({ id: foundUser.id}, config.secret, {
expiresIn: 86400 // expires in 24 hours
});
res.status(200).json({auth:true,token:token});
}
} catch (err) {
res.status(500).json(err);
}
}
my json
我在 index js 上为 jsonenter 图像描述实现了 body-parser,并且路由工作,因为 userId 和 pwd 值使其成为 findOne 查询。但是它仍然说查询后没有结果。终端运行以下(userId 是“slim”)
执行(默认): SELECT
id
、 username
、 userId
、 pw
、 createdAt
、 updatedAt
FROM users
AS users
WHERE users
。 userId
= 'slim';'slim' 既在 Json 中又在 db 的 'userId' 部分
最佳答案
事实证明,问题是因为我的数据库的 userId 部分中不止一个“slim”,这很有趣。当我为 userId 输入一个真正唯一的值时,它按预期返回了 token 。也许这看起来很明显,但重要的是该值要求唯一的身份验证。
关于mysql - 当 usedIf 在数据库中时,sequelize findOne 找不到 userId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62102299/