小编cam*_*iam的帖子

扩展服务类时如何处理NestJS依赖注入?

我正在尝试根据我提供的价值提供其他服务ConfigService

我遇到的问题是,在执行诸如findOne()(result is null)或countDocuments()(result is 0)之类的查询方法时,注入的猫鼬模型不会返回任何值。

我的服务类定义如下:

    export class BaseService {
      constructor(@InjectModel('Cat') public readonly catModel: Model<Cat>) {}

      createService(option: string) {
        if (option === 'OTHER') {
          return new OtherService(this.catModel);
        } else if (option === 'ANOTHER') {
          return new AnotherService(this.catModel);
        } else {
          return new BaseService(this.catModel);
        }
      }

      async findOne(id: string): Promise<Cat> {
        return await this.catModel.findOne({_id: id});
      }

      async count(): Promise<number> {
        return await this.catModel.countDocuments();
      }

      testClass() {
        console.log('BASE SERVICE CLASS USED');
      }
    } …
Run Code Online (Sandbox Code Playgroud)

extends dependency-injection subclass mongoose nestjs

9
推荐指数
1
解决办法
517
查看次数