0

Whats is the correct way to work with Mongoose and GridFS?

I'm trying to add Mongodb Gridfs to NestJS Example 14 (https://github.com/nestjs/nest/tree/master/sample/14-mongoose-base)

But when I use the tag @InjectConnection:

  // files.service.ts
  private readonly fileModel: MongoGridFS;
  constructor(@InjectConnection() private readonly connection: Connection) {
    this.fileModel = new MongoGridFS(this.connection.db, 'fs');
  }
  async readStream(id: string): Promise<GridFSBucketReadStream> {
    return await this.fileModel.readFileStream(id);
  }

The following error occurs:

[Nest] 24282   - 04/20/2020, 4:10:23 PM   [ExceptionHandler] Nest can't resolve dependencies of the FilesService (?). Please make sure that the argument DatabaseConnection at index [0] is available in the FilesModule context.

Potential solutions:
- If DatabaseConnection is a provider, is it part of the current FilesModule?
- If DatabaseConnection is exported from a separate @Module, is that module imported within FilesModule?
  @Module({
    imports: [ /* the Module containing DatabaseConnection */ ]
  })

Error: Nest can't resolve dependencies of the FilesService (?). Please make sure that the argument DatabaseConnection at index [0] is available in the FilesModule context.
4

1 回答 1

0

由于MongooseModule不是@Global(),您需要在需要数据库连接的任何地方导入模块。在您的示例中,您只有MulterModule正在导入的内容,因此当您尝试执行时@InjectConnection(),Nest 无法找到来源MongooseModuleConnection来源,从而引发错误。导入MongooseModule以解决此问题。

于 2020-04-21T04:09:59.730 回答