[英]Unhandled promise rejection error value is not defined in Node.js
我们正在尝试在Node.js Express中修复此创建用户控制器。 问题是,当您单击前端的注册时,它将引发此错误:未定义的未处理的承诺拒绝错误值。 在第28行显示,但看起来并不是主要问题。 这是代码:
async CreateUser(req, res) {
const userEmail = await User.findOne({
email: req.body.email
});
if (userEmail) {
return res
.status(HttpStatus.CONFLICT)
.json({ message: 'Email already exist' });
}
const userName = await User.findOne({
username: req.body.username
});
if (userName) {
return res
.status(HttpStatus.CONFLICT)
.json({ message: 'Username already exist' });
}
return bcrypt.hash(value.password, 10, (err, hash) => {
if (err) {
return res
.status(HttpStatus.BAD_REQUEST)
.json({ message: 'Error hashing password' });
}
const body = {
username: value.username,
email: value.email,
password: hash
};
User.create(body)
.then(user => {
res
.status(HttpStatus.CREATED)
.json({ message: 'User created successfully', user, token });
})
.catch(() => {
res
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.json({ message: 'Error occured' });
});
});
},
如何解决呢?
错误: value is not defined
未处理的承诺拒绝错误value is not defined
。
在您的代码中看到: 价值在哪里? ,应该是req.body.password
return bcrypt.hash(value.password, 10, (err, hash) => {
if (err) {
return res
.status(HttpStatus.BAD_REQUEST)
.json({ message: 'Error hashing password' });
}
const body = {
username: value.username,
email: value.email,
password: hash
};
要更新的代码:
return bcrypt.hash(req.body.password, 10, (err, hash) => {
if (err) {
return res
.status(HttpStatus.BAD_REQUEST)
.json({ message: 'Error hashing password' });
}
const body = {
username: req.body.username,
email: req.body.email,
password: hash
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.