BCrypt TypeError: Cannot read property 'value' of undefined NodeJs
BCrypt TypeError: Cannot read property 'value' of undefined NodeJs
我正在尝试为我的密码加盐,但出现以下错误:
(node:12652) UnhandledPromiseRejectionWarning: TypeError: Cannot read
property 'value' of undefined
(node:12652) 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(). (rejection id: 1)
(node:12652) [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.
我的auth/index.js
const bcrypt = require('bcrypt');
const saltRounds = 10;
const myPlaintextPassword = 's0/\/\P4$$w0rD';
const someOtherPlaintextPassword = 'not_bacon';
const salt = bcrypt.genSaltSync(saltRounds);
exports.modules = {
salt
}
我的控制器:
const Users = require('../models/users');
const bcrypt = require('bcrypt');
const { salt } = require('../auth/index');
const getUsers = ((req,res) =>
Users.findAll()
.then((result) => {
res.json(result)
})
.catch((error) => { res.json(error) })
)
const addUsers = (async (req,res,next) => {
const name = req.body.name;
const email = req.body.email;
let password = bcrypt.hashSync(req.body.password, salt.value);
const data = {
name,
email,
password
};
console.log(data);
Users.create(data)
.then((result) => { res.json(result) })
.catch((error) => { res.json(error) });
});
module.exports = {
getUsers,
addUsers,
Users
}
您导出了一个 salt
函数。您正在尝试访问它的对象,这当然是未定义的,因为该对象没有 属性 名称 value
。因此,它会给您一个 UnhandledPromiseRejectionWarning
错误。 Salt
函数已经 return 一个值。
还有一件事,应该是auth/index.js
中的module.exports
。
您应该 return 来自函数的值。这就是您可以重写整个过程的方式。
index.js
const bcrypt = require('bcrypt');
const saltRounds = 10;
const myPlaintextPassword = 's0/\/\P4$$w0rD';
const someOtherPlaintextPassword = 'not_bacon';
module.exports.salt= function(){
return bcrypt.genSaltSync(saltRounds); }
您可以等待控制器级别的加盐功能。
let password = await bcrypt.hashSync(req.body.password, salt());
就是这样。现在,我认为您的代码会起作用。
我正在尝试为我的密码加盐,但出现以下错误:
(node:12652) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'value' of undefined
(node:12652) 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(). (rejection id: 1)
(node:12652) [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.
我的auth/index.js
const bcrypt = require('bcrypt');
const saltRounds = 10;
const myPlaintextPassword = 's0/\/\P4$$w0rD';
const someOtherPlaintextPassword = 'not_bacon';
const salt = bcrypt.genSaltSync(saltRounds);
exports.modules = {
salt
}
我的控制器:
const Users = require('../models/users');
const bcrypt = require('bcrypt');
const { salt } = require('../auth/index');
const getUsers = ((req,res) =>
Users.findAll()
.then((result) => {
res.json(result)
})
.catch((error) => { res.json(error) })
)
const addUsers = (async (req,res,next) => {
const name = req.body.name;
const email = req.body.email;
let password = bcrypt.hashSync(req.body.password, salt.value);
const data = {
name,
email,
password
};
console.log(data);
Users.create(data)
.then((result) => { res.json(result) })
.catch((error) => { res.json(error) });
});
module.exports = {
getUsers,
addUsers,
Users
}
您导出了一个 salt
函数。您正在尝试访问它的对象,这当然是未定义的,因为该对象没有 属性 名称 value
。因此,它会给您一个 UnhandledPromiseRejectionWarning
错误。 Salt
函数已经 return 一个值。
还有一件事,应该是auth/index.js
中的module.exports
。
您应该 return 来自函数的值。这就是您可以重写整个过程的方式。
index.js
const bcrypt = require('bcrypt');
const saltRounds = 10;
const myPlaintextPassword = 's0/\/\P4$$w0rD';
const someOtherPlaintextPassword = 'not_bacon';
module.exports.salt= function(){
return bcrypt.genSaltSync(saltRounds); }
您可以等待控制器级别的加盐功能。
let password = await bcrypt.hashSync(req.body.password, salt());
就是这样。现在,我认为您的代码会起作用。