Javascript 非法参数:未定义,字符串
我在注册用户时遇到此错误: (节点:13225)未处理的PromisejectionWarning:错误:非法 参数:未定义,字符串 位于Object.bcrypt.hashSync(/home/admin/Desktop/project/node_modules/bcryptjs/dist/bcrypt.js:189:19) 在module.exports.register(/home/admin/Desktop/project/controllers/auth.js:26:30)(节点:13225) 未处理的PromisejectionWarning:未处理的承诺拒绝。这 错误源于异步函数的内部抛出 没有拦截,或拒绝未处理的承诺 with.catch()(拒绝id:1) 控制器:Javascript 非法参数:未定义,字符串,javascript,node.js,Javascript,Node.js,我在注册用户时遇到此错误: (节点:13225)未处理的PromisejectionWarning:错误:非法 参数:未定义,字符串 位于Object.bcrypt.hashSync(/home/admin/Desktop/project/node_modules/bcryptjs/dist/bcrypt.js:189:19) 在module.exports.register(/home/admin/Desktop/project/controllers/auth.js:26:30)(节点:13
const bcrypt = require('bcryptjs')
const jwt = require('jsonwebtoken')
const db = require('../config/db.config.js')
const User = db.user
const errorHandler = require('../utils/errorHandler')
module.exports.register = async function(req, res) {
const candidate = await User.findOne({
where: {
username: req.body.username
}
})
if (candidate) {
res.status(409).json({
message: 'This login is already taken. Try another.'
})
} else {
const salt = bcrypt.genSaltSync(10)
const password = req.body.password
const user = new User({
name: req.body.name,
username: req.body.username,
roles: req.body.roles,
photoSrc: req.file ? req.file.path: '',
password: bcrypt.hashSync(password, salt)
})
try {
await user.save()
res.status(201).json(user)
} catch(e) {
errorHandler(res, e)
}
}
}
型号:
module.exports = (sequelize, Sequelize) => {
const User = sequelize.define('users', {
name: {
type: Sequelize.STRING(100),
required: true
},
username: {
type: Sequelize.STRING(40),
required: true,
unique: true
},
roles: {
type: Sequelize.STRING(100),
required: true
},
password: {
type: Sequelize.STRING,
required: true
},
photoSrc: {
type: Sequelize.STRING(200),
default: ''
}
});
return User;
}
您需要将
wait
应用到您的salt
和password
作业中
像这样,
const salt = await bcrypt.genSaltSync(10);
const password = await req.body.password;
希望这有帮助 以这种方式为我工作,您可以使用async Wait代替cb
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash("password", salt, function(err, hash) {
// Store hash in your password DB.
});
});
谢谢 对于每个异步操作,我们必须等待
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(req.body.password, salt);
我在使用lambda函数时遇到了相同的错误,但我的问题是解析req主体,所以我必须
const body = JSON.parse(event.body) // in case of lambda function
希望这对其他人有所帮助。这发生在我验证注册表时,我正在搜索修复程序,但没有得到正确的解决方案 我是如何修复的 后来我意识到,在发送post请求之前,我没有通过必填字段
单击上面的链接查看示例。这是我的最佳答案