【问题标题】:Post Api not return any response in nest jsPost Api 在嵌套 js 中不返回任何响应
【发布时间】:2021-05-18 03:20:14
【问题描述】:

我使用 nestjs 和 psql,我想上传文件并将 url 保存在数据库中。当我运行 api 时,数据保存在 db 上,但没有返回任何响应。 这是我的服务:

 async uploadFiles(files){
    if (!files) {
      throw new HttpException(
        {
          errorCode: UploadApplyOppErrorEnum.FileIsNotValid,
          message: UploadApplyOppMsgEnum.FileIsNotValid,
        },
        HttpStatus.UNPROCESSABLE_ENTITY,
      );
    }
    const filedata = OrderFilesData(files);
     return filedata.map(async(filePath) => {
      let orderFile = new OrderFile();
      orderFile.fileUrl = filePath.fileUrl;
      orderFile.type = filePath.fileType;
      try {
         let result = await this.orderFileRepository.save(orderFile);         
         return await result
      } catch (error) {
        throw new BadRequestException(error.detail);
      }
    });
  }

这是我的控制器

  @UploadOrderFilesDec()
  @Post('upload')
  uploadFiles(@UploadedFiles() files){
     return this.ordersService.uploadFiles(files);
  }

【问题讨论】:

    标签: postgresql nestjs


    【解决方案1】:

    如果不使用Promise.all(),则不能返回异步方法数组,否则承诺尚未解决。您可以使用return Promise.all(fileData.map(asyncFileMappingFunction)),也可以使用常规的for 循环并等待结果。

    【讨论】:

      猜你喜欢
      • 2017-03-15
      • 1970-01-01
      • 2021-04-25
      • 2021-01-15
      • 2020-10-23
      • 1970-01-01
      • 1970-01-01
      • 2021-04-17
      • 2017-12-08
      相关资源
      最近更新 更多