Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError when using disableMiddleware with I18nValidationExceptionFilter #606

Open
5 tasks done
myself7890 opened this issue Dec 30, 2023 · 0 comments
Open
5 tasks done

Comments

@myself7890
Copy link

myself7890 commented Dec 30, 2023

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

  • Follow our Code of Conduct
  • Read the Contributing Guide.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
  • The provided reproduction is a minimal reproducible of the bug.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant