-1

i am using nest-i18n package to use localization in my project but when translating the message i get the error Cannot read properties of undefined (reading 'service')

Here is my code please help me to find out the exact error

Note:- I have tried to add I18nValidationPipe as a provider in app module as well but still gives me same error


main.ts

    import 'dotenv/config'
    import { ValidationPipe } from '@nestjs/common'
    import { LazyModuleLoader, NestFactory } from '@nestjs/core'
    import { ConfigService } from '@nestjs/config'
    import { WinstonModule } from 'nest-winston'
    import {
    LoggerTransports,
    BaseMessage,
    from './utilities'
    import * as express from 'express'
    import * as winston from 'winston'
    import { AppModule } from './app.module'
    import {
    I18nValidationExceptionFilter,
    I18nValidationPipe,
    i18nValidationErrorFactory,
    from 'nestjs-i18n'

    async function bootstrap() {
    const appLogger = WinstonModule.createLogger({
    format: winston.format.uncolorize(),
    transports: LoggerTransports,
    -     })

    const app = await NestFactory.create(AppModule, {
    cors: true,
    bodyParser: true,
    -     })
    const configService = app.get(ConfigService)

    app.use(express.urlencoded({ extended: true }))

    Lazy Loading Module
    const lazyModuleLoader = app.get(LazyModuleLoader)
    await lazyModuleLoader.load(() => AppModule)

    app.useLogger(appLogger)


    app.useGlobalPipes(new I18nValidationPipe())

    app.useGlobalFilters(new I18nValidationExceptionFilter())

    app.useGlobalFilters(new AppExceptionFilter(appLogger))
    app.useGlobalInterceptors(new TransformInterceptor(appLogger))

    -     //Initiating Server
    const port = configService.get('server.port')
    await app.listen(port, '0.0.0.0')
    appLogger.log(BaseMessage.ServerStartUp + port)
    - }

    bootstrap()


app.module.ts
  
    import { Module } from '@nestjs/common'
    import { AppController } from './app.controller'
    import { AppService } from './app.service'
    import { ConfigModule, ConfigService } from '@nestjs/config'
    import { environment } from './config/environment'
    import { TypeORMConfigFactory } from './utilities'
    import { TypeOrmModule } from '@nestjs/typeorm'
    import { EmployeeModule } from './modules/employee/employee.module'
    import { GraphQLModule } from '@nestjs/graphql'
    import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo'
    import { join } from 'path'
    import { AuthModule } from './modules/auth/auth.module'
    import {
        I18nModule,
        HeaderResolver,
    } from 'nestjs-i18n'

    @Module({
        imports: [
            ConfigModule.forRoot({
                isGlobal: true,
                cache: true,
                ignoreEnvFile: true,
                load: [environment],
            }),
            TypeOrmModule.forRootAsync({
                imports: [ConfigModule],
                inject: [ConfigService],
                useFactory: TypeORMConfigFactory,
            }),
            I18nModule.forRootAsync({
                useFactory: (configService: ConfigService) => ({
                    fallbackLanguage: 'en',
                    fallbacks: {
                        fr: 'fr',
                        en: 'en',
                    },
                    loaderOptions: {
                        path: join(__dirname, '/i18n'),
                        watch: true,
                    },
                }),
                resolvers: [
                    // GraphQLWebsocketResolver,
                    // { use: QueryResolver, options: ['lang'] },
                    // AcceptLanguageResolver,
                    new HeaderResolver(['x-custom-lang']),
                ],
                inject: [ConfigService],
            }),
            GraphQLModule.forRoot<ApolloDriverConfig>({
                driver: ApolloDriver,
                autoSchemaFile: join(process.cwd(), 'src/schema/graphql.gql'),
                definitions: {
                    path: join(process.cwd(), 'src/graphql.ts'),
                },
                playground: true,
                // installSubscriptionHandlers: true,
                context: ({ req }) => ({ req }),
            }),
            EmployeeModule,
            AuthModule,
        ],
        controllers: [AppController],
        providers: [AppService],
    })
    export class AppModule {}


create-employee.args.ts
   
    import { InputType, Field } from '@nestjs/graphql'
    import { Transform } from 'class-transformer'
    import {
        IsEmail,
        IsNotEmpty,
        IsString,
    } from 'class-validator'
    @InputType()
    export class CreateEmployeeArg {
        @IsNotEmpty({
            message: 'validation.NOT_EMPTY',
        })
        @Field()
        @IsString()
        fullName: string

        @Field()
        @IsNotEmpty()
        @IsString()
        username: string

        @Field()
        @IsNotEmpty()
        @IsString()
        @Transform(({ value }) => value.toLowerCase())
        @IsEmail()
        email: string
    }

validation.json
    {
      "fullName":"{property} is required!",
      "NOT_EMPTY":"name is required!"
    }
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Aug 22 '23 at 07:58

0 Answers0