I get this error when registering a user:
(node:13225) UnhandledPromiseRejectionWarning: Error: Illegal arguments: undefined, string at Object.bcrypt.hashSync (/home/admin/Desktop/project/node_modules/bcryptjs/dist/bcrypt.js:189:19) at module.exports.register (/home/admin/Desktop/project/controllers/auth.js:26:30) (node:13225) 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)
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)
}
}
}
models:
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;
}
I get this error when registering a user:
(node:13225) UnhandledPromiseRejectionWarning: Error: Illegal arguments: undefined, string at Object.bcrypt.hashSync (/home/admin/Desktop/project/node_modules/bcryptjs/dist/bcrypt.js:189:19) at module.exports.register (/home/admin/Desktop/project/controllers/auth.js:26:30) (node:13225) 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)
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)
}
}
}
models:
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;
}
Share
Improve this question
asked Oct 25, 2018 at 6:39
user10493107user10493107
1411 gold badge3 silver badges11 bronze badges
7 Answers
Reset to default 5You need to apply await
to your salt
and password
assignments too.
Like this,
const salt = await bcrypt.genSaltSync(10);
const password = await req.body.password;
Hope this helps!.
For every async operation, we have to await
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(req.body.password, salt);
This happened with me when I was verifying my registration form, I was searching for the fix but didn't get a proper solution.
How I fixed it Later I realised that I have not passed the required fields before sending post request. Before Sending Post request through Postman make sure You have passed key and value in postman
Click on Above link to see example.
I was missing the "OTP" field in Schema
Old:
Schema({
email: {
type: String,
requird: true,
},
createdAt: {
type: Date,
default: Date.now,
index: { expires: 300 },
// after 5 mins it get's deleted
},
},
{ timestamps: true })
New:
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 })
working for me like this way, instead of cb, you can use async-await
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash("password", salt, function(err, hash) {
// Store hash in your password DB.
});
});
Thanks!!
I encountered the same error using lambda function, but the problem with me was parsing the req body so i had to
const body = JSON.parse(event.body) // in case of lambda function
hope this helps someone.
I faced the same problem. It was due to two issues:
- I switched from
bcryptjs
tobcrypt
, which worked perfectly for hashing:
const salt = await bcrypt.genSalt(10); // Generate salt
const hashedPassword = await bcrypt.hash(password, salt);
- My database configuration was causing issues. I replaced
node:sqlite
withbetter-sqlite3
, and it solved the problem:
import Database from "better-sqlite3";
// Create or connect to a SQLite database file
const db = new Database("mydatabase.sqlite");
Try these solutions; they worked for me!