如何将Mongoose和GridFS集成到NestJS?

问题描述 投票:0回答:1

使用Mongoose和GridFS的正确方法是什么?

我正在尝试将Mongodb Gridfs添加到NestJS示例14(https://github.com/nestjs/nest/tree/master/sample/14-mongoose-base

但是当我使用标签@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);
  }

发生以下错误:

[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.
node.js mongodb nestjs gridfs multer-gridfs-storage
1个回答
0
投票

由于MongooseModule不是@Global(),因此您需要在需要数据库连接的任何位置导入模块。在您的示例中,仅导入了MulterModule,因此当您尝试执行@InjectConnection()时,Nest无法找到MongooseModule的来源Connection,因此会引发错误。导入MongooseModule修复此问题。

© www.soinside.com 2019 - 2024. All rights reserved.