Как интегрировать Mon goose и GridFS в Nest JS? - PullRequest
0 голосов
/ 21 апреля 2020

Как правильно работать с Mon goose и GridFS?

Я пытаюсь добавить Mongodb Gridfs в Nest JS Пример 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.

1 Ответ

0 голосов
/ 21 апреля 2020

Поскольку MongooseModule не @Global(), вам нужно будет импортировать модуль туда, где вам нужно подключение к базе данных. В вашем примере импортируется только MulterModule, поэтому, когда вы пытаетесь выполнить @InjectConnection(), Nest не может найти MongooseModule, откуда берется Connection, и, таким образом, выдает ошибку. Импортируйте MongooseModule, чтобы исправить это.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...