javascript - bcrypt 密码比较在带有 mongoose 的 Node.js 中不起作用
问题描述
每当我用Postman
to发送一个帖子请求时/api/user/login
。它显示user.password
未定义。我正在尝试将普通密码与存储在其中的现有哈希密码进行比较,MongoDB
但它显示的是ReferenceError: user is not defined
.
以下是代码和带有屏幕截图的错误消息。请让我知道我在哪里搞砸了。
const router = require('express').Router();
const User = require('../model/User');
const bcrypt = require('bcryptjs');
const Joi = require('@hapi/joi');
const registerSchema = Joi.object({
name: Joi.string()
.min(6)
.required(),
email: Joi.string()
.min(6)
.email()
.required(),
password: Joi.string()
.min(6)
.required()
})
const loginSchema = Joi.object({
email:Joi.string()
.required(),
password:Joi.string()
.required()
})
router.post('/register', async(req, res)=>{
const {error} = registerSchema.validate(req.body);
if(error)
return res.status(400).send(error.details[0].message);
// Checking if the user exist in database
const checkExistingEmail = await User.findOne({email: req.body.email});
if(checkExistingEmail) return res.status(400).send('Email already exist');
// Hash passwords
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(req.body.password, salt);
//Create New Database for user
const user = new User({
name: req.body.name,
email: req.body.email,
password: hashedPassword
});
try {
const savedUser = await user.save()
res.send({user : user._id});
} catch(err) {
res.status(400).send(err);
}
});
router.post('/login', async(req, res) =>{
const {error} = loginSchema.validate(req.body)
if(error)
return res.status(400).send(error.details[0].message);
// Checking if the user exist in database
const checkExistingEmail = await User.findOne({email: req.body.email});
if(!checkExistingEmail) return res.status(400).send('Email does not exist');
// Check Password
const validPass = await bcrypt.compare(req.body.password, user.password);
if(!validPass) return res.status(400).send('Password does not match');
res.send('Logged In');
});
module.exports = router;
错误显示在这里:
[nodemon] 2.0.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node app.js`
Server running and listening at port 8081....
Connected to Database...
(node:9380) UnhandledPromiseRejectionWarning: ReferenceError: user is not defined
at D:\tapu\PROJECT WORKS\PROJECT 1.0\Personal Blogging Web Application\Server Side\Login API\routes\auth.js:67:67
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:9380) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:9380) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate
the Node.js process with a non-zero exit code.
解决方案
您应该首先学习如何阅读堆栈跟踪。
UnhandledPromiseRejectionWarning: ReferenceError: user is not defined at D:\tapu\PROJECT WORKS\PROJECT 1.0\Personal Blogging Web Application\Server Side\Login API\routes\auth.js:67:67
auth.js
第 67 行文件中的代码中有 ReferenceError 。而不是chekcExistingEmail
您使用user
变量。
您可以在此处阅读有关堆栈跟踪的更多信息:https ://www.digitalocean.com/community/tutorials/js-stack-trace
推荐阅读
- java - 如何在回收站视图中显示更新和加载的文本?
- java - 我应该在 Builder Pattern 中使用原始数据类型还是包装数据类型
- ruby-on-rails - 用户中的 NoMethodError #显示用户的未定义方法“以下”
- html - 在背景图像上放置不可见链接
- azure-cosmosdb - cosmosdb 上的唯一稀疏索引不起作用
- html - 根据内容/标签优化按钮宽度
- powershell - 将字符串拆分为两个数组
- svg - 如何给三角形的两条边上色?
- jquery - 反应(cra):当尝试在 fullcalendar 的“eventRender”中使用 jQuery 时,我收到错误:$el.popover 不是函数
- erlang - 以记录为键的映射 - 如何检查 Erlang 中是否存在键