我的登录路由中有一个请求失败,返回了500错误。

前端开发 2026-07-07

嗯,这个登录路由是不是有问题?我检查了一下bcrypt.compare是否写对了,结果确实能工作,而且我甚至使用了try catch,但现在出现一个错误,提示请求失败,状态码401。

下面是控制器方法

   static async login(req,res){
        try{
        const email=req.body.email
        const password=req.body.password
        const user=prisma.user.findUnique({where:{email:email}})
        if(!user || !bcrypt.compare(passworld,user.password)){
           res.status(401).json({msg:"invalid credentials"})
        }
        const token=jwt.sign({id:user._id},process.env.JWT_SECRET,{expiresIn:'7d'})
        res.status(201).json({token})
    }
catch(err){
    res.status(401).json({msg:"invalid credentials"})
}
    }

解决方案

变量 password 拼写错误:

bcrypt.compare(passworld,user.password)
                      ^

该变量被拼写为“pass world”,而不是“pass word”。

服务器返回500错误意味着“未处理的异常”,因此你需要在响应或应用日志中查找异常信息和堆栈跟踪。

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章