【问题标题】:Make multiple httpService requests in NestJs在 NestJs 中发出多个 httpService 请求
【发布时间】:2022-01-25 10:46:39
【问题描述】:

我正在使用 NestJs @nestjs/axios HttpModule 向下面的控制器发出 POST 请求:

@Controller('api')
export class ApiController {
    constructor(private apiService: ApiService) {}

    @Post()
    async acceptAllRequest(@Body() data: string) {
        return this.apiService.executeAllRequest(data);
    }
}

传递给控制器​​的数据包含我想传递给服务以获取这些 URL 端点并作为单个响应返回的 URL:

@Injectable()
export class ApiService {

    constructor(private httpService: HttpService) { }

    async executeAllRequest(data: string): Promise<any> {
        // convert the data string into json object
        // have an array of URL that I want to GET and return as single response
    }
}

有什么办法吗?我遇到了使用rxjs forkjoin 但我无法将其作为单个响应返回。我需要你们更多的指导,提前谢谢你们:)

【问题讨论】:

  • 您希望所有这些请求顺序发生还是并行发生?
  • 如果可以的话,我很想学习顺序和并行方式

标签: typescript axios rxjs nestjs endpoint


【解决方案1】:

//Write this code in service.ts

        async findOne(_id: number): Promise<any> {
          const new_req = await this.yourRepository.findOne({id: _id});
          return {
            StatusCode: HttpStatus.CREATED,
            message: "",
            result: new_req
          }

//and then write this code on your controller.ts

//For Get single request

  @Get('/:id')
  getSinglRequest(
    @Param('id') _id: number): Promise<any> {
    return this.yourService.findOne(_id);
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-16
    • 2019-08-21
    • 2021-12-10
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    • 2020-10-13
    • 2022-11-24
    相关资源
    最近更新 更多