Skip to content

Commit 3e35b35

Browse files
committed
fix: add env variable active
1 parent 8533d1c commit 3e35b35

5 files changed

Lines changed: 29 additions & 9 deletions

File tree

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
"@nestjs/platform-express": "^11.0.1",
2727
"@nestjs/swagger": "^11.1.5",
2828
"@types/joi": "^17.2.2",
29-
"dotenv": "^16.5.0",
3029
"joi": "^17.13.3",
3130
"mongoose": "^8.13.2",
3231
"reflect-metadata": "^0.2.2",
@@ -51,6 +50,7 @@
5150
"@typescript-eslint/eslint-plugin": "^8.30.1",
5251
"@typescript-eslint/parser": "^8.30.1",
5352
"concurrently": "^9.1.2",
53+
"dotenv": "^16.5.0",
5454
"eslint": "^9.18.0",
5555
"eslint-config-prettier": "^10.0.1",
5656
"eslint-plugin-prettier": "^5.2.2",

src/app.module.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
import { Module } from '@nestjs/common';
22
import { MongooseModule } from '@nestjs/mongoose';
3-
import { ConfigModule } from '@nestjs/config';
3+
import { ConfigModule, ConfigService } from '@nestjs/config';
44
import { NotificationModule } from './notification/notification.module';
55
import { LoggerModule } from './logger/logger.module';
66

77
@Module({
88
imports: [
9-
ConfigModule.forRoot({ isGlobal: true }),
10-
MongooseModule.forRoot(
11-
process.env.MONGO_URI || 'mongodb://localhost:27017/smartedu',
12-
),
9+
ConfigModule.forRoot({
10+
isGlobal: true,
11+
envFilePath: '.env', // Explicit path to .env
12+
ignoreEnvFile: false, // Ensure it's not ignored
13+
}),
14+
MongooseModule.forRootAsync({
15+
imports: [ConfigModule],
16+
useFactory: (configService: ConfigService) => {
17+
const uri = configService.get<string>('MONGO_URL');
18+
if (!uri) {
19+
throw new Error('MONGO_URI not found in configuration');
20+
}
21+
return { uri };
22+
},
23+
inject: [ConfigService],
24+
}),
1325
NotificationModule,
1426
LoggerModule,
1527
],

src/main.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ import { AllExceptionsFilter } from './filters/all-exceptions.filter';
44
import { LoggerServiceFile } from './logger/services/logger.service.file';
55
import { LoggerServiceDb } from './logger/services/logger.service.db';
66
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
7+
import * as dotenv from 'dotenv';
8+
import * as path from 'path';
9+
10+
// 1. Load .env file manually first (as backup)
11+
const envPath = path.resolve(__dirname, '../.env');
12+
dotenv.config({ path: envPath });
13+
14+
console.log('Environment variables loaded from:', process.env.MONGO_URL);
715

816
async function bootstrap() {
917
const app = await NestFactory.create(AppModule);

src/notification/presentation/controllers/log.controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,10 @@ export class LogController {
162162
async getLogStats(
163163
@Query('level') level?: string,
164164
@Query('context') context?: string,
165-
): Promise<{ data: Record<string, unknown> }> {
165+
): Promise<{ data: Record<string, string> }> {
166166
const stats = (await this.logService.getLogStats(level, context)) as Record<
167167
string,
168-
unknown
168+
string
169169
>;
170170
return { data: stats };
171171
}

0 commit comments

Comments
 (0)