Mongoose middleware not triggered at all


I am trying to pre save and hash password with bcrypt in mongoose in my next.js project, but password still unhashed. i tryed every link in stackoverflow and didnt solve it, the password still saved unHashed. mongoose version: 6.9.1

this is my users.model file:

import {
} from 'mongoose';
import bcrypt from 'bcrypt';

const UserSchema: Schema = new Schema({
  email: {
    type: String,
    required: true,
    unique: true,
  password: {
    type: String,
    required: true,
  displayName: {
    type: String,
    required: true,
  role: {
    type: String,

UserSchema.pre('save', function (next) {
  console.log('Pre-Save Hash has fired.');
  let user = this;
  bcrypt.genSalt(10, (err, salt) => {
    if (err) console.error(err);
    bcrypt.hash(user.password, salt, (err, hash) => {
      user.password = hash;

const UserModel = models.Users || model('Users', UserSchema, 'users');

export default UserModel;

this is my adding function file:

import dbConnect from '@/utils/mongodb';
import UserModel from '@/models/user.model';
import { NextApiRequest, NextApiResponse } from 'next';
import { MongoError } from 'mongodb';
export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  // const { email, password } = req.query;
  try {
    const query = req.body;
    const newUser = new UserModel(query);
    const addedUser= await (err: MongoError) {
      if (err) {
        throw err;
  } catch (error) {
    res.status(500).json({ message: 'Internal server error' });

i cant see the ‘Pre-Save Hash has fired.’ in my console also…

any help please?

Same Happen with me Plz anyone have solution