我正在尝试创建使用我的Nest上下文的外部CLI。我已经建立了入口点,该入口点使用NestFactory.create创建应用程序。使用app.get访问服务后,该服务存在并可以正常工作。问题是,它不能解决任何依赖关系。文档上没有示例,我也没有在Internet上找到与此问题相关的任何信息。
我正在2019年7月17日在Node 10上使用Nest.js的最新版本。
main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from '../app.module';
import { UploadService } from 'src/api/upload/upload.service';
import { UploadModule } from 'src/api/upload/upload.module';
async function bootstrap(): Promise<void> {
const app = await NestFactory.create(AppModule);
await app.init();
const service: UploadService = app.select(UploadModule).get(UploadService);
console.log(service); // OK - returns UploadService instance
console.log(service.uploadModel); // X - returns `undefined`
console.log(service.configService); // X - returns `undefined`
}
bootstrap();
app.module.ts
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { ConfigModule } from './config/config.module';
import { ConfigService } from './config/config.service';
import { AwsModule } from './services/aws/aws.module';
import { UploadModule } from './api/upload/upload.module';
@Module({
imports: [
ConfigModule,
MongooseModule.forRootAsync({
useFactory: async (configService: ConfigService): Promise<object> => ({
uri: configService.data.database.mongo.uri,
useCreateIndex: true,
useNewUrlParser: true,
}),
inject: [ConfigService],
}),
AwsModule,
UploadModule,
],
})
export class AppModule {}
upload.module.ts
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { AwsModule } from '../../services/aws/aws.module';
import { UploadService } from './upload.service';
import { UploadSchema } from './upload.schema';
@Module({
imports: [MongooseModule.forFeature([{ name: 'Upload', schema: UploadSchema }]), AwsModule],
providers: [UploadService],
exports: [UploadService],
})
export class UploadModule {}
upload.service.ts
import { Injectable } from '@nestjs/common';
import { AwsService } from '../../services/aws/aws.service';
import { Upload } from './upload.schema';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
@Injectable()
export class UploadService {
constructor(
@InjectModel('Upload')
readonly uploadModel: Model<Upload>,
readonly awsService: AwsService,
) {}
}
预期的输出
console.log(service.uploadModel); // X - returns `undefined`
console.log(service.configService); // X - returns `undefined`
是模型/服务实例。不幸的是,由于依赖项注入没有发生,它们都返回了undefined
。
答案 0 :(得分:0)
我尝试了你所做的,在 nestjs 版本:7.6 中它按预期工作。
所以我的假设是他们同时修复了它。