BCrypt TypeError:无法读取未定义的NodeJ的属性“值”

用户名

我正在尝试在密码中添加盐,但出现此错误:

(节点:12652)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“值”

(节点:12652)UnhandledPromiseRejectionWarning:未处理的承诺被拒绝。该错误是由于在没有catch块的情况下抛出异步函数而引起的,或者是由于拒绝了.catch()未处理的承诺而导致的。(拒绝ID:1)

(节点:12652)[DEP0018] DeprecationWarning:已弃用未处理的承诺拒绝。将来,未处理的承诺拒绝将以非零退出代码终止Node.js进程。

我的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函数已经返回一个值。

还有一件事,它应该在module.exportsauth/index.js

您应该从函数返回一个值。这就是您可以重写整个过程的方式。

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());

而已。现在,我认为您的代码可以使用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TypeError:无法读取未定义的属性“ then”

未捕获的TypeError:无法读取未定义的属性“值”

从bcrypt哈希函数获取未定义的值

“ TypeError:无法读取未定义的属性'then'

无法安装bcrypt python

Bcrypt在Lumen 5.4中不起作用:调用未定义函数bcrypt()

使用bcrypt时对象未定义

无法读取未定义的bcrypt.hash()属性“ then”

动态形式:TypeError:无法读取未定义的属性“值”

REACT TypeError:无法读取未定义的属性“值”

无法使用bcrypt比较密码

TypeError:无法读取未定义的属性“未定义”

TypeError:无法读取REACTJS中未定义的属性“值”

“ TypeError:无法读取未定义的属性”

如何修复'TypeError:无法读取未定义的属性'then'TypeError:无法读取未定义的属性'then'...'

Bcrypt无法工作,Bcrypt.compareSync总是返回false

使用bcrypt-nodejs的未定义函数

使用“bcrypt”的密码散列方法返回未定义

Angular 6:TypeError:无法读取未定义的属性“值”

Reactjs:TypeError:无法读取未定义的属性“值”

Bcrypt 哈希函数返回未定义的结果

ReactJS TypeError:无法读取未定义的属性“值”

ReactJS TypeError:无法读取未定义的属性(读取“地图”)

反应 - 错误:TypeError:无法读取未定义的属性(读取“then”)

“TypeError:无法读取未定义的属性(读取'id')”

TypeError:无法读取未定义的属性(读取“有”)

TypeError:无法读取未定义的属性(读取“等于”)

React TypeError:无法读取未定义的属性(读取“状态”)

TypeError:无法读取未定义的属性(读取“pageNumber”)