Reputation: 141
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;
}
Upvotes: 5
Views: 43145
Reputation: 1
I faced the same problem. It was due to two issues:
bcryptjs
to bcrypt
, which worked perfectly for hashing:const salt = await bcrypt.genSalt(10); // Generate salt
const hashedPassword = await bcrypt.hash(password, salt);
node:sqlite
with better-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!
Upvotes: 0
Reputation: 107
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 })
Upvotes: 2
Reputation: 39
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.
Upvotes: 2
Reputation: 919
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.
Upvotes: 0
Reputation: 56
For every async operation, we have to await
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(req.body.password, salt);
Upvotes: 2
Reputation: 98
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!!
Upvotes: 0
Reputation: 15639
You 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!.
Upvotes: 5