[英]Illegal arguments: undefined, string
注册用户时出现此错误:
(node:13225) UnhandledPromiseRejectionWarning: Error: Illegal arguments: undefined, string at Object.bcrypt.hashSync (/home/admin/Desktop/project/node_jsports/bcryptjs/dist/b.exports:18js19) atcrypt module.注册 (/home/admin/Desktop/project/controllers/auth.js:26:30) (node:13225) UnhandledPromiseRejectionWarning: 未处理的 promise 拒绝。 此错误源于在没有 catch 块的情况下抛出异步 function 内部,或拒绝未使用.catch() 处理的 promise。(拒绝 id:1)
controller:
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;
}
您也需要将await
应用于salt
和password
分配。
像这样,
const salt = await bcrypt.genSaltSync(10);
const password = await req.body.password;
希望这可以帮助!。
对于每个异步操作,我们必须等待
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(req.body.password, salt);
像这样为我工作,而不是 cb,你可以使用 async-await
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash("password", salt, function(err, hash) {
// Store hash in your password DB.
});
});
谢谢!!
我在使用 lambda 函数时遇到了同样的错误,但我的问题是解析 req 主体,所以我不得不
const body = JSON.parse(event.body) // in case of lambda function
希望这有助于某人。
当我验证我的注册表时发生了这种情况,我正在寻找修复程序但没有得到正确的解决方案。
我是如何修复的后来我意识到在发送 post 请求之前我没有通过必填字段。 在通过Postman发送Post 请求之前,请确保您已经在 postman 中传递了key 和 value
单击上面的链接查看示例。
我错过了架构中的“OTP”字段
老的:
Schema(
{
email: {
type: String,
requird: true,
},
createdAt: {
type: Date,
default: Date.now,
index: { expires: 300 },
// after 5 mins it get's deleted
},
},
{ timestamps: true }
)
新的:
Schema(
{
email: {
type: String,
requird: true,
},
otp: {
type: String,
required: true,
},
createdAt: {
type: Date,
default: Date.now,
index: { expires: 300 },
// after 5 mins it get's deleted
},
},
{ timestamps: true }
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.