Undefined genSalt() or genSaltSync()
kingrootid opened this issue · comments
import { bcrypt } from 'bcrypt';
const saltRounds = 10;
@Injectable()
export class UsersService {
constructor(
@InjectRepository(Users)
private readonly usersRepository: Repository<Users>,
) {}
getHello(): string {
return 'Hello World!';
}
async findAll(): Promise<Users[]> {
return this.usersRepository.find();
}
async create(createUserDto: CreateUsersDto): Promise<Users> {
var salt = await bcrypt.genSaltSync(saltRounds);
const user = new Users();
user.name = createUserDto.name;
user.email = createUserDto.email;
user.password = await bcrypt.hashSync(createUserDto.password, salt);
user.address = createUserDto.address;
user.subdomain = createUserDto.subdomain;
user.phone = createUserDto.phone;
try {
return this.usersRepository.save(user);
} catch (err) {
return err.message;
}
}
}
i use nestjs to hash user password.
i got an error
[Nest] 5532 - 03/23/2022, 1:00:53 PM ERROR [ExceptionsHandler] Cannot read properties of undefined (reading 'genSaltSync')
TypeError: Cannot read properties of undefined (reading 'genSaltSync')
You should be doing import bcrypt from 'bcrypt'