Berk Disli
Berk Disli

Reputation: 1

{ "Users validation failed: password: Cast to string failed for value \"Promise { <pending> }\" (type Promise) at path \"password\"" }

I have always an error when I'm trying to create a user from postman: { "message": "Users validation failed: password: Cast to string failed for value "Promise { }" (type Promise) at path "password"" }

Terminal: C:\Users\Asus\Documents\GitHub\users\node_modules\bcryptjs\dist\bcrypt.js:214 nextTick(callback.bind(this, Error("Illegal arguments: "+(typeof s)+', '+(typeof salt)))); ^

Error: Illegal arguments: undefined, number at _async (C:\Users\Asus\Documents\GitHub\users\node_modules\bcryptjs\dist\bcrypt.js:214:46)
at C:\Users\Asus\Documents\GitHub\users\node_modules\bcryptjs\dist\bcrypt.js:223:17 at new Promise () at Object.bcrypt.hash (C:\Users\Asus\Documents\GitHub\users\node_modules\bcryptjs\dist\bcrypt.js:222:20) at generateHashPassword (C:\Users\Asus\Documents\GitHub\users\src\helpers\securePassword.js:5:25) at addUser (C:\Users\Asus\Documents\GitHub\users\src\controllers\users.js:51:23) at processTicksAndRejections (node:internal/process/task_queues:96:5)

var bcrypt = require('bcryptjs');

const generateHashPassword = async (plainPassword) => {
    const saltRounds = 10;
    return await bcrypt.hash(plainPassword, saltRounds)
}

const compareHashPassword = async (plainPassword, hashedPassword) => {
    return await bcrypt.compare(plainPassword, hashedPassword)
}


const addUser = async (req, res) => {
    try {
        const isExist = await User.findOne({ email: req.body.email });
        if (isExist)
            return res.status(400).json({
                message: `the user already exist`
            })

        const newUser = new User({
            id: getUniqueId(),
            name: req.body.name,
            email: req.body.email,
            age: req.body.age,
            password: generateHashPassword(req.body.password)
        });
        const user = await newUser.save();
        if (!user)
            return res.status(400).json({
                message: "user was not created"
            });

        return res.status(201).json({
            message: "user was created"
        })
    } catch (err) {
        return res.status(500).json({
            message: err.message
        })
    }
};

I thought that it is all about bcryptjs, but couldn't solve it. Could someone please help me to solve this issue?

Note: Also tried to create a promise to hash, but didn't work:

const generateHashPassword = async (plainPassword) => {
    const saltRounds = 10;
    return new Promise(async (resolve, reject) => {
        const hash = await bcrypt.hash(plainPassword, saltRounds)
        if (hash == this.plainPassword) {
            return resolve(true);
        }

        return reject();
    });
}

Upvotes: 0

Views: 652

Answers (1)

Arubisou Handel
Arubisou Handel

Reputation: 1

The issue is with your saltRound

use const saltround = await bcryptjs.genSalt(10)

Upvotes: 0

Related Questions