Facing the problem in code while trying to secure the password using bcrypt.

When I used bcrypt to hash the password in my backend I am getting a particular error here whenever the user hit submit on my registration page. I have tried adding findByEmail but that only worsens the situation doesn’t check wether user exists or not.

I am using MySql, node(express)
const bcrypt = require(‘bcrypt’);
const saltRounds = 10; // or any other suitable number of salt rounds

     class User {
    constructor(name, email, password) {
    this.name = name;
    this.email = email;
    this.password = password;
}

async save() {
    try {
        if (!this.name || !this.email || !this.password) {
            throw new Error('Name, email, and password are required.');
        }

        const hashedPassword = await bcrypt.hash(this.password, saltRounds);
        const query = `INSERT INTO users (name, email, password) VALUES (?, ?, ?)`;
        const result = await db.query(query, [this.name, this.email, hashedPassword]);
        return result;
    } catch (error) {
        // Handle error appropriately, e.g., send an error response
        console.error(error);
        return { error: 'Failed to register user. Please try again.' };
    }
}

}

New contributor

Abhishek Srivastava is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.