toonvanstrijp / nestjs-i18n

The i18n module for nestjs.
https://nestjs-i18n.com
Other
656 stars 113 forks source link

TypeError when using disableMiddleware with I18nValidationExceptionFilter #606

Open myself7890 opened 11 months ago

myself7890 commented 11 months ago

Describe the bug

When setting disableMiddleware to true (so it can run my guards first THEN the interceptor, which is the order I need), and use DTO validation translation (with I18nValidationExceptionFilter), I get the following error:

TypeError: Cannot read properties of undefined (reading 'service')
at I18nValidationExceptionFilter.catch (/app/apps/backend/node_modules/nestjs-i18n/dist/filters/i18n-validation-exception.filter.js:26:83)
at ExceptionsHandler.invokeCustomFilters (/app/apps/backend/node_modules/@nestjs/core/exceptions/exceptions-handler.js:30:26)
at ExceptionsHandler.next (/app/apps/backend/node_modules/@nestjs/core/exceptions/exceptions-handler.js:14:18)
at /app/apps/backend/node_modules/@nestjs/core/router/router-proxy.js:13:35
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Reproduction

This is my code in app.module.ts:

@Module({
  imports: [
    I18nModule.forRootAsync({
      useFactory: () => ({
        fallbackLanguage: 'en',
        loaderOptions: {
          path: path.join(__dirname, '/i18n/'),
          watch: true,
        },
        disableMiddleware: true,
      }),
      resolvers: [
        new QueryResolver(['lang']),
        LangResolver,
        AcceptLanguageResolver,
      ],
      imports: [UsersModule],
    }),
  ],
  controllers: [],
  providers: [],
})
export class AppModule {}

lang.resolver.ts:

@Injectable()
export class LangResolver implements I18nResolver {
  constructor(private usersService: UsersService) {}

  async resolve(context: ExecutionContext): Promise<string | undefined> {
    const req = context.switchToHttp().getRequest();

    const userID = req.user?.ID;
    const user = await this.usersService.findOneByID(userID);

    return user?.lang?.locale;
  }
}

main.ts

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalPipes(new I18nValidationPipe({ transform: true }));
  app.useGlobalFilters(new I18nValidationExceptionFilter());
  app.setGlobalPrefix('api');
  app.enableCors();

  useContainer(app.select(AppModule), { fallbackOnErrors: true });

  await app.listen(8080);
}
bootstrap();

System Info

System:
    OS: Linux 5.15 Ubuntu 20.04.5 LTS (Focal Fossa)
    CPU: (8) x64 Intel(R) Core(TM) i5-10210U CPU @ 1.60GHz
    Memory: 888.42 MB / 5.70 GB
    Container: Yes
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 18.16.0 - ~/.nvm/versions/node/v18.16.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v18.16.0/bin/yarn
    npm: 9.5.1 - ~/.nvm/versions/node/v18.16.0/bin/npm

Used Package Manager

yarn

Validations