nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.63k stars 7.62k forks source link

NestJs and Mongoose ClassSerializerInterceptor return empty response #12851

Closed UXProv closed 11 months ago

UXProv commented 11 months ago

Is there an existing issue for this?

Current behavior

I have a project in nestJs, with mongosee, and I want to exclude certain properties from returned object. Specifically:

@Schema()
export class User {
  @Prop({type: String, required: true, unique: true, index:true})
  email: string;

  @Prop({type: String, required: true})
  username: string;

  @Prop({type: String, required: true, unique: true, index:true})
  handle: string;

  @Prop({type: String, required: true})
  @Exclude({ toPlainOnly: true })
  password: string;

  @Prop({type: String, default: () => randomBytes(16).toString('hex')})
  @Exclude({ toPlainOnly: true })
  emailVerifCode: string;

  //other properties
}

And when I try this:

@Post('login')
  @UseGuards(AuthGuardLocal)
  @UseInterceptors(ClassSerializerInterceptor)
  async login(@CurrentUser() user: User) {

    return {
      user: user,
      token: await this.authService.getTokenForUser(user)
    }
  }

I get returned an empty object. I also tried adding:

  app.useGlobalInterceptors(
    new ClassSerializerInterceptor(app.get(Reflector))
  );

All controllers I tried with ClassSerializerInterceptor returns an empty object.

To the main.ts but I get again an empty object as a response. I tried other solutions but none worked. Getting kind of desperate at this point.

Using: "mongoose": "^7.5.2", "@nestjs/core": "^10.0.0"

Minimum reproduction code

https://github.com/UXProv/squadron-backend-debug

Steps to reproduce

No response

Expected behavior

Retrieve the user without the @Exclude fields

Package

Other package

No response

NestJS version

v10.00.00

Packages versions

{
  "name": "nest-boilerplate-mongo-auth",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@getbrevo/brevo": "^1.0.1",
    "@hapi/joi": "^17.1.1",
    "@nestjs/common": "^10.0.0",
    "@nestjs/config": "^3.1.1",
    "@nestjs/core": "^10.0.0",
    "@nestjs/jwt": "^10.1.1",
    "@nestjs/mapped-types": "^2.0.2",
    "@nestjs/mongoose": "^10.0.1",
    "@nestjs/passport": "^10.0.2",
    "@nestjs/platform-express": "^10.0.0",
    "@types/mongoose": "^5.11.97",
    "aws-sdk": "^2.1473.0",
    "bcrypt": "^5.1.1",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.0",
    "mime": "^3.0.0",
    "mongoose": "^7.5.2",
    "nodemailer": "^6.9.5",
    "passport": "^0.6.0",
    "passport-google-oauth20": "^2.0.0",
    "passport-jwt": "^4.0.1",
    "passport-local": "^1.0.0",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.8.1",
    "sib-api-v3-typescript": "^2.2.2"
  },
  "devDependencies": {
    "@nestjs/cli": "^10.0.0",
    "@nestjs/schematics": "^10.0.0",
    "@nestjs/testing": "^10.2.6",
    "@types/bcrypt": "^5.0.0",
    "@types/express": "^4.17.17",
    "@types/jest": "^29.5.2",
    "@types/multer": "^1.4.8",
    "@types/node": "^20.3.1",
    "@types/passport-jwt": "^3.0.9",
    "@types/supertest": "^2.0.12",
    "@typescript-eslint/eslint-plugin": "^6.0.0",
    "@typescript-eslint/parser": "^6.0.0",
    "dotenv": "^16.3.1",
    "eslint": "^8.42.0",
    "eslint-config-prettier": "^9.0.0",
    "eslint-plugin-prettier": "^5.0.0",
    "jest": "^29.5.0",
    "prettier": "^3.0.0",
    "source-map-support": "^0.5.21",
    "supertest": "^6.3.3",
    "ts-jest": "^29.1.0",
    "ts-loader": "^9.4.3",
    "ts-node": "^10.9.1",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.1.3"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "test",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

Node.js version

v20.9.0

In which operating systems have you tested?

Other

.

UXProv commented 11 months ago

Edit: Updated the git repository to an open one. Deleted all unused modules and env variables, left db variable to allow testing (I'm using atlas and that DB is currently running). DB and secret will be changed after issue is resolved. To create a user in postman:

localhost:3000/user/register
{
    "email" : "email@email.com",
    "username" : "testuser",
    "handle" : "test",
    "password" : "Test12345.",
    "retypedPassword" : "Test12345."
}

To test the empty return:


{
    "username": "test",
    "password" : "Test12345."
}```
kamilmysliwiec commented 11 months ago

Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.