使用 bcryptjs 将散列密码保存到数据库时出错
error saving hashed password to db using bcryptjs
我正在使用 node.js 和 bcryptjs 注册新用户并将他们的 name/email/password 保存到 mlab 中的 mongoDB。
这是我的代码
const express = require("express");
const router = express.Router();
const gravatar = require("gravatar");
const bcrypt = require("bcryptjs");
// Load User model
const User = require("../../models/User");
// @route GET api/users/test
// @desc Tests users route
// @access Public
router.get("/test", (req, res) => res.json({ msg: "Users Works" }));
// @route GET api/users/register
// @desc Register user
// @access Public
router.post("/register", (req, res) => {
User.findOne({ email: req.body.email }).then(user => {
if (user) {
return res.status(400).json({ email: "Email already exists" });
} else {
const avatar = gravatar.url(req.body.email, {
s: "200", // Size
r: "pg", // Rating
d: "mm" // Default
});
const newUser = new User({
name: req.body.name,
email: req.body.email,
avatar,
password: req.body.password
});
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(newUser.password, salt, (err, hash) => {
if (err) throw err;
newUser.password = hash;
newUser
.save()
.then(user => res.json(user))
.catch(err => console.log(err));
});
});
}
});
});
// @route GET api/users/login
// @desc Login User / Returning JWT Token
// @access Public
module.exports = router;
如果我注释掉第 37 行 "if(err) throw err;" 我可以存储用户凭据,但不会存储密码(使用邮递员)
我收到的错误是..
错误:非法参数:未定义,字符串
在 _async (C:\Users\Cody\Desktop\DevSoc\node_modules\bcryptjs\dist\bcrypt.js:214:46)
在 Object.bcrypt.hash(C:\Users\Cody\Desktop\DevSoc\node_modules\bcryptjs\dist\bcrypt.js:220:13)
在 bcrypt.genSalt(C:\Users\Cody\Desktop\DevSoc\routes\api\users.js:36:16)
在 Immediate._onImmediate(C:\Users\Cody\Desktop\DevSoc\node_modules\bcryptjs\dist\bcrypt.js:153:21)
在 runCallback (timers.js:789:20)
在 tryOnImmediate (timers.js:751:5)
在 processImmediate [as _immediateCallback] (timers.js:722:5)
这是从哪里来的?我在我的代码中看不到错误。
谢谢
您似乎正在将 undefined
密码传递给 bcrypt.hash
。
为确保它不会到达那个点,添加较早的验证,可能在 POST 路由的第一行:
router.post("/register", (req, res) => {
const {email, password} = req.body
if (!email || !password) {
return res.send('Must include email and password')
}
...
})
我正在使用 node.js 和 bcryptjs 注册新用户并将他们的 name/email/password 保存到 mlab 中的 mongoDB。
这是我的代码
const express = require("express");
const router = express.Router();
const gravatar = require("gravatar");
const bcrypt = require("bcryptjs");
// Load User model
const User = require("../../models/User");
// @route GET api/users/test
// @desc Tests users route
// @access Public
router.get("/test", (req, res) => res.json({ msg: "Users Works" }));
// @route GET api/users/register
// @desc Register user
// @access Public
router.post("/register", (req, res) => {
User.findOne({ email: req.body.email }).then(user => {
if (user) {
return res.status(400).json({ email: "Email already exists" });
} else {
const avatar = gravatar.url(req.body.email, {
s: "200", // Size
r: "pg", // Rating
d: "mm" // Default
});
const newUser = new User({
name: req.body.name,
email: req.body.email,
avatar,
password: req.body.password
});
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(newUser.password, salt, (err, hash) => {
if (err) throw err;
newUser.password = hash;
newUser
.save()
.then(user => res.json(user))
.catch(err => console.log(err));
});
});
}
});
});
// @route GET api/users/login
// @desc Login User / Returning JWT Token
// @access Public
module.exports = router;
如果我注释掉第 37 行 "if(err) throw err;" 我可以存储用户凭据,但不会存储密码(使用邮递员)
我收到的错误是..
错误:非法参数:未定义,字符串 在 _async (C:\Users\Cody\Desktop\DevSoc\node_modules\bcryptjs\dist\bcrypt.js:214:46) 在 Object.bcrypt.hash(C:\Users\Cody\Desktop\DevSoc\node_modules\bcryptjs\dist\bcrypt.js:220:13) 在 bcrypt.genSalt(C:\Users\Cody\Desktop\DevSoc\routes\api\users.js:36:16) 在 Immediate._onImmediate(C:\Users\Cody\Desktop\DevSoc\node_modules\bcryptjs\dist\bcrypt.js:153:21) 在 runCallback (timers.js:789:20) 在 tryOnImmediate (timers.js:751:5) 在 processImmediate [as _immediateCallback] (timers.js:722:5)
这是从哪里来的?我在我的代码中看不到错误。
谢谢
您似乎正在将 undefined
密码传递给 bcrypt.hash
。
为确保它不会到达那个点,添加较早的验证,可能在 POST 路由的第一行:
router.post("/register", (req, res) => {
const {email, password} = req.body
if (!email || !password) {
return res.send('Must include email and password')
}
...
})