Package version: 10.2.1
Error:
[I18nService] No resolvers provided! nestjs-i18n won't workt properly, please follow the quick-start guide: https://nestjs-i18n.com/quick-start
Code:
#Module({
imports: [
ConfigModule.forRoot({ cache: true, isGlobal: true }),
I18nModule.forRootAsync({
inject: [ConfigService],
useFactory: (configService: ConfigService) => ({
fallbackLanguage: 'en',
loaderOptions: {
path: join(__dirname, 'i18n'),
watch: configService.get('NODE_ENV') === 'development',
},
logging: configService.get('NODE_ENV') === 'development', // REVIEW: Is this necessary?
resolvers: [AcceptLanguageResolver],
}),
}),
],
})
Revert to the old version.
Resolved!
#Module({
imports: [
ConfigModule.forRoot({ cache: true, isGlobal: true }),
I18nModule.forRootAsync({
inject: [ConfigService],
resolvers: [AcceptLanguageResolver],
useFactory: (configService: ConfigService) => ({
fallbackLanguage: 'en',
loaderOptions: {
path: join(__dirname, 'i18n'),
watch: configService.get('NODE_ENV') === 'development',
},
logging: configService.get('NODE_ENV') === 'development', // REVIEW: Is this necessary?
}),
}),
],
})
Related
This code is to integrate NestJs on sentry .
**SentryModule.forRootAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
dsn: configService.get<string>('sentry.dsn'),
debug: true,
environment: configService.get<string>('sentry.env'),
logLevels: ['error'],
}),
inject: [ConfigService],
})
],
providers: [
{
provide: APP_INTERCEPTOR,
useValue: new SentryInterceptor(),
},
],
controllers: [AppController],
})
export class AppModule {}**
I need to integrate performance monitoring in NestJs on sentry.
I am working on a nestjs project using Kafka microservices (import from #nestjs/microservices).
for listening to a message, I am using the following code in main.ts:
const app = await NestFactory.createMicroservice<MicroserviceOptions>(
AppModule,
{
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
transport: Transport.KAFKA,
options: {
client: {
brokers: configService.get('brokers'),
},
},
}),
inject: [ConfigService],
},
);
await app.listen();
I am trying to let the nestjs read brokers from .env.
It can not work.
and I got error :
Argument of type '{ imports: (typeof ConfigModule)[]; useFactory: (configService: ConfigService) => Promise<{ transport: Transport; options: { client: { brokers: any; }; }; }>; inject: (typeof ConfigService)[]; }' is not assignable to parameter of type 'NestApplicationContextOptions & MicroserviceOptions'.
Object literal may only specify known properties, and 'imports' does not exist in type 'NestApplicationContextOptions & MicroserviceOptions'.
if delete 'imports: [ConfigModule]', I have the following error:
Argument of type '{ useFactory: (configService: ConfigService) => Promise<{ transport: Transport; options: { client: { brokers: any; }; }; }>; inject: (typeof ConfigService)[]; }' is not assignable to parameter of type 'NestApplicationContextOptions & MicroserviceOptions'.
Object literal may only specify known properties, and 'useFactory' does not exist in type 'NestApplicationContextOptions & MicroserviceOptions'.
Please help :)
as I see that you included config module at root level, and you did not initialize it. I think you should add Config Module like below.
imports: [
ConfigModule.forRoot({
isGlobal: true,
}),
someModule,
fooModule,
],
useFactory: async (configService: ConfigService) => ({
transport: Transport.KAFKA,
options: {
client: {
brokers: configService.get('brokers'),
},
},
}),
inject: [ConfigService],
})
I am relatively new to using typeorm. I am trying to separate the ormconfig.json for production and development environment but I keep getting this error:
Error during migration generation:
Error: Cannot find connection default because its not defined in any orm configuration files.
Folder structure:
database.module.ts
import { Module } from '#nestjs/common';
import { TypeOrmModule } from '#nestjs/typeorm';
import { Connection, getConnectionOptions } from 'typeorm';
#Module({
imports: [
TypeOrmModule.forRootAsync({
useFactory: async () =>
Object.assign(
await getConnectionOptions(
process.env.NODE_ENV === 'production' ? 'prod' : 'dev',
),
),
}),
],
exports: [TypeOrmModule],
})
export class DatabaseModule {
constructor(connection: Connection) {
if (connection.isConnected) console.log('DB Connected Successfully!');
}
}
App.module.ts
...
#Module({
imports: [
ConfigModule.forRoot({ isGlobal: true }),
DatabaseModule,
GraphQLModule.forRoot({
playground: true,
debug: true,
autoSchemaFile: true,
}),
ComponentsModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
ormconfig.json
[
{
"name": "dev",
"type": "postgres",
"host": "localhost",
"port": 5432,
"username": "...",
"password": "...",
"database": "yourcar",
"entities": ["dist/**/entities/*{.ts,.js}"],
"synchronize": true,
"logging":true,
"migrationsRun":true,
"migrations":["dist/database/migrations/*{.ts,.js}"],
"cli": {
"migrationsDir":"src/database/migrations"
}
},
{
"name": "prod",
"type": "postgres",
"host": "localhost",
"port": 5432,
"username": "...",
"password": "...",
"database": "yourcar",
"entities": ["dist/**/entities/*{.ts,.js}"],
"synchronize": false,
"migrationsRun":true,
"migrations":["dist/database/migrations/*{.ts,.js}"],
"cli": {
"migrationsDir":"src/database/migrations"
}
}
]
I thought the ormconfig.json gets automatically detected at the root directory so I tried using just one environment and changed database.module.ts to:
#Module({
imports: [TypeOrmModule.forRoot(typeOrmConfig)],
})
export class DatabaseModule {
constructor(connection: Connection) {
if (connection.isConnected) {
console.log('CONNECTED SUCCESS');
}
}
}
but this also doesn't work so I guess ormconfig is not getting detected only. Any help is greatly appreciated.
You can use NestJS/config with .env file for declare your BDD config. And in your database.module.ts you can set this config.
Example :
// app.module.ts
#Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
cache: true,
load: config,
envFilePath: getEnvFiles(),
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
// src/config/database.config.ts
export default registerAs('database', () => ({
host: process.env.DB_HOST || 'localhost',
port: process.env.DB_PORT || 3306,
user: process.env.DB_USER || 'root',
password: process.env.DB_PASSWORD || 'root',
database: process.env.DB_DATABASE || 'test',
...
}));
// database.module.ts
#Module({
imports: [
TypeOrmModule.forRootAsync({
inject: [ConfigService],
useFactory: async (configService: ConfigService) => ({
type: 'postgres',
host: configService.get<string>('database.host'),
port: configService.get<number>('database.port'),
username: configService.get<string>('database.user'),
password: configService.get<string>('database.password'),
database: configService.get<string>('database.database'),
logger: new CustomORMLogger(new AdvancedConsoleLogger('all'), configService),
logging: configService.get<boolean>('database.logging'),
entities: configService.get<string[]>('database.entities'),
migrations: configService.get<string[]>('database.migrations'),
synchronize: true,
}),
}),
],
exports: [TypeOrmModule],
})
export class DatabaseModule {
constructor(connection: Connection) {
if (connection.isConnected) console.log('DB Connected Successfully!');
}
}
I am learning NestJS and I am trying to test a service which incorporates a repository with TypeORM. It compiles fine without errors, but when I run the test it throw an error.
I know it is a test dependency problem but I can't figure it out. I am trying this test:
describe('JokesService', () => {
let service: JokesService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [],
imports: [
TypeOrmModule.forFeature([JokeEntity])
],
}).compile();
service = module.get<JokesService>(JokesService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});
The module:
#Module({
providers: [JokesService],
imports: [
TypeOrmModule.forFeature([JokeEntity])
],
controllers: [JokesController],
})
export class JokesModule {}
The service:
#Injectable()
export class JokesService {
constructor(
#InjectRepository(JokeEntity)
private readonly jokeRepository: MongoRepository<JokeEntity>
) {}
}
The main module:
#Module({
imports: [
TypeOrmModule.forRoot({
type: 'mongodb',
url: dbUri,
entities: [__dirname + '/**/*.entity{.ts,.js}'],
ssl: true,
useUnifiedTopology: true,
useNewUrlParser: true,
}),
JokesModule
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
And the error:
Nest cant resolve dependencies of the JokeEntityRepository (?).
Please make sure that the argument Connection at index [0] is available in the TypeOrmModule context.
Potential solutions:
- If Connection is a provider, is it part of the current TypeOrmModule?
- If Connection is exported from a separate #Module, is that module imported within TypeOrmModule?
#Module({
imports: [ /* the Module containing Connection */ ]
})
Can someone tell me what I am missing in the test dependencies? Thank You.
Try to import your JokesModule inside your app.module.
#Module({
imports: [
TypeOrmModule.forRoot({
type: 'mongodb',
url: dbUri,
entities: [__dirname + '/**/*.entity{.ts,.js}'],
ssl: true,
useUnifiedTopology: true,
useNewUrlParser: true,
}),
JokesModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
When running heroku config I see (with some omissions)
DATABASE_URL: postgres://<xxxx>:<xxxx>#ec2-xx-xx-xxx-xx.compute-1.amazonaws.com:5432/dm74hmu71b97n
which I know is of form postgres://<user>:<password>#<hostname>:<port>/<database>. But right now in my Nest.JS app I connect to postgres like this:
#Module({
imports: [
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (configService: ConfigService) => ({
type: 'postgres',
host: configService.get('POSTGRES_HOST'),
port: configService.get('POSTGRES_PORT'),
username: configService.get('POSTGRES_USER'),
password: configService.get('POSTGRES_PASSWORD'),
database: configService.get('POSTGRES_DB'),
entities: [__dirname + '/../**/*.entity{.ts,.js}'],
synchronize: false,
}),
}),
],
})
export class DatabaseModule {}
I suppose I could manually parse process.env.DATABASE_URL I figured there must be a better/easier way.
useFactory: (configService: ConfigService) => ({
url: configService.get('DATABASE_URL'),
type: 'postgres',
entities: [__dirname + '/../**/*.entity{.ts,.js}'],
synchronize: false,
}),