如何将 Mongoose 和 GridFS 集成到 NestJS 中?
How to integrate Mongoose and GridFS to NestJS?
使用 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.
由于 MongooseModule
不是 @Global()
,您需要在需要数据库连接的任何地方导入该模块。在您的示例中,您只导入了 MulterModule
,因此当您尝试执行 @InjectConnection()
时,Nest 找不到 Connection
来自的 MongooseModule
,因此抛出一个错误。导入 MongooseModule
来解决这个问题。
使用 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.
由于 MongooseModule
不是 @Global()
,您需要在需要数据库连接的任何地方导入该模块。在您的示例中,您只导入了 MulterModule
,因此当您尝试执行 @InjectConnection()
时,Nest 找不到 Connection
来自的 MongooseModule
,因此抛出一个错误。导入 MongooseModule
来解决这个问题。