title: Cannot hash the user's password with Prisma middleware in Nestjs on create User API request
body: <p>I want to hash the user's password on create API with Prisma middleware, but it does not work. I create a separate ts file for Prisma middleware and import it into the main.ts file.</p>
  <p>here is my code:</p>
  <p><strong>Prisma Middleware:</strong></p>
  <pre><code>import { Prisma, PrismaClient } from '@prisma/client';
  import * as bcrypt from 'bcrypt';
  
  export function passwordMiddleware() {
    const prisma: PrismaClient = new PrismaClient();
  
    prisma.$use(async (params: Prisma.MiddlewareParams, next) =&gt; {
      if (params.action == 'create' &amp;&amp; params.model == 'User') {
        const user = params.args.data;
        const salt = bcrypt.genSaltSync(10);
        const hash = bcrypt.hashSync(user.password, salt);
        user.password = hash;
        params.args.data = user;
      }
      return await next(params);
    });
  }
  </code></pre>
  <p><strong>main.ts file:</strong></p>
  <pre><code>import { Logger } from '@nestjs/common';
  import { NestFactory } from '@nestjs/core';
  
  import { AppModule } from './app/app.module';
  import { passwordMiddleware } from './app/core/prisma/middleware';
  
  async function bootstrap() {
    const app = await NestFactory.create(AppModule);
    const globalPrefix = 'api';
    app.setGlobalPrefix(globalPrefix);
    const port = process.env.PORT || 3333;
    //******** Start Prisma Middlewares ********
    passwordMiddleware();
    //******** End Prisma Middlewares ********
    await app.listen(port, () =&gt; {
      Logger.log('Listening at http://localhost:' + port + '/' + globalPrefix);
    });
  }
  
  bootstrap();
  </code></pre>
  <p><strong>Prisma Model:</strong></p>
  <pre><code>model User {
    id         String      @id @default(uuid())
    email      String      @unique
    password   String
    name       String?
    gender     String?
    address    String?
    telecom    String?
    birthDate  DateTime?
    createdAt DateTime @default(now())
  }
  </code></pre>
  <p><strong>Auth Controller:</strong></p>
  <pre><code>  @Post('register')
    register(@Body() registerDto: RegisterDto) {
      return this.authService.register(registerDto);
    }
  </code></pre>
  <p><strong>Auth Service:</strong></p>
  <pre><code>register(registerDto: RegisterDto): Promise&lt;User&gt; {
      return this.prismaService.user.create({
        data: {
          email: registerDto.email,
          password: registerDto.password,
        },
      });
    }
  </code></pre>