0

I recieve password through req.body and hash it like this :

    const { name, email, password, number } = req.body;

    let userExist;
    userExist = await User.find({ email: email });

    if (userExist.length) {
        const err = new Error('User already exists');
        return next(err);
    }

    let hashedPass;
    try {
        hashedPass = await bcrypt(req.body.password, 12);
    } catch (e) {
        console.log('error here bro');
        return next(e);
    }

    const createdUser = new User({
        name,
        email,
        password: hashedPass,
        number,
        ads: []
    });

It console logs 'error here bro' and I dont know why .

How can I hash password coming from user with bcrypt js ?

1
  • 1
    Tip: console.error(e) to find out what the error is, bro. Commented Sep 9, 2020 at 22:14

2 Answers 2

1

You can also add salt to your password in the following way:

const salt = await bcrypt.genSalt(7);
const hashPassword = await bcrypt.hash(req.body.secret, salt);
Sign up to request clarification or add additional context in comments.

Comments

0

Solved the problem by adding hash after bcrypt

bcrypt.hash(....)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.