mongodb - 使用 NestJS 和 GraphQL 访问 MongoDB GridFS 中的文件
问题描述
我正在使用带有 GraphQL 和 MongoDB 的 NestJS。我正在尝试使用 GridFS 使用mongo-gridfs
包存储图像文件。
将图像上传到数据库可以正常工作,但是如何访问这些文件?我的意思是,例如我想获取这些文件的源路径并在我的前端使用它
这是我的解析器和服务:
// photo.resolver.ts
import { Resolver, Mutation, Query, Args } from '@nestjs/graphql';
import { GraphQLUpload, FileUpload } from 'graphql-upload';
import { Photo } from './dto/photo.dto';
import { PhotoService } from './services/photo.service';
@Resolver()
export class PhotoResolver {
constructor(
private readonly photoService: PhotoService
) {}
@Query(() => ???, { nullable: true })
async photo(@Args('id', { nullable: true }) id: string) {
const photo = await this.photoService.findOne(id);
return ???;
}
@Mutation(() => Photo)
async uploadPhoto(@Args({name: 'file', type: () => GraphQLUpload}) file: FileUpload) {
return await this.photoService.save(file);
}
}
// photo.service.ts
import { Connection } from 'mongoose';
import { Injectable } from '@nestjs/common';
import { InjectConnection } from '@nestjs/mongoose';
import { FileUpload } from 'graphql-upload';
import { MongoGridFS } from 'mongo-gridfs';
import { Photo } from '../photo.interface';
@Injectable()
export class PhotoService {
private fileModel: MongoGridFS;
constructor(
@InjectConnection() private readonly connection: Connection
) {
this.fileModel = new MongoGridFS(this.connection.db as any, 'photo');
}
async findOne(id: string) {
return await this.fileModel.findById(id);
}
async save(file: FileUpload): Promise<Photo> {
return await this.fileModel.writeFileStream(file.createReadStream(), {
filename: file.filename,
contentType: file.mimetype
});
}
}
我试过两种方法:
- 我使用
downloadFile
了 my 中的方法photoModel
,但它返回Temp
本地磁盘目录中此文件的路径。
// photo.service.ts
async findOne(id: string): Promise<string> {
return await this.fileModel.downloadFile(id); // C:\...\AppData\Local\Temp\189450ef
}
// photo.resolver.ts
@Query(() => String, { nullable: true })
async photo(@Args('id', { nullable: true }) id: string) {
return id && await this.photoService.findOne(id);
}
它本身可以工作,但在我看来它不是一个合适的解决方案。我希望源路径应该“引导”到我的服务器。
- 我使用
readFileStream
了我的方法photoModel
,它返回文件流并添加@Res() res
到解析器中的参数。
// photo.service.ts
async findOne(id: string): Promise<GridFSBucketReadStream> {
return await this.fileModel.readFileStream(id);
}
// photo.resolver.ts
@Query(() => Boolean)
async photo(@Args('id', { nullable: true }) id: string, @Res() res) {
const photoStream = await this.photoService.findOne(id);
photoStream.pipe(res);
return true;
}
现在我在终端中遇到了这样的错误:
[Nest] 12408 - 10.07.2021, 13:02:25 [ExceptionsHandler] dest.on is not a function +27555ms
TypeError: dest.on is not a function
解决方案
推荐阅读
- python - Python:函数中的常量变量值,而不是变量
- javascript - 变量css不适用于反应js
- java - 图像未显示在 firebase 的 recyclerview 中
- typescript - Cypress/TypeScript:在创建对象时将值传递给类函数
- parent - 如何在使用 windows.open 打开子弹出窗口时禁用父浏览器窗口访问
- tibco - 最小化多个 Tibco 代理/目的地的带宽
- elasticsearch - 弹性搜索 - 6.3.2
- reactjs - 需要帮助来设置/塑造一个新项目
- node.js - websocket 连接到 'wss://url_from_aws_domain/socket.io/?EIO=4&transport=websocket' 失败?
- python - 我如何获得不和谐的消息并将其输入到 python 终端