Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 以Typescript、Angular和Rxjs返回多个HttpClients_Javascript_Angular_Typescript_Rxjs - Fatal编程技术网

Javascript 以Typescript、Angular和Rxjs返回多个HttpClients

Javascript 以Typescript、Angular和Rxjs返回多个HttpClients,javascript,angular,typescript,rxjs,Javascript,Angular,Typescript,Rxjs,我需要向多个端点发出get请求,但希望一次返回所有端点。 这将返回单个端点的get请求: public getTasks(): Observable<any> { this.logger.info('TasksService: getTasks()'); const endpoint = `${this.processEngineUriPrefix}runtime/tasks`; // https://www.flowable.org/docs/usergui

我需要向多个端点发出get请求,但希望一次返回所有端点。 这将返回单个端点的get请求:

public getTasks(): Observable<any> {

   this.logger.info('TasksService: getTasks()');

   const endpoint = `${this.processEngineUriPrefix}runtime/tasks`;

   // https://www.flowable.org/docs/userguide/index.html#_request_parameters
   const sort = 'createTime';
   const order = 'asc'; // 'desc'
   // const start = 0;
   // const size = 16;

   const params = new HttpParams().set('sort', sort).set('order', order);

   return this.httpClient.get<TaskListModel>(endpoint, this.getHttpOptions(params)).pipe(

     tap(() => {

       this.logger.info('TasksService: getTasks() completed');

     }),
     catchError(error => {

       this.logger.info('TasksService: getTasks() -> catchError()');

       if (error === undefined) {

         error = new Error(HTTP_SERVER_ERROR_CONNECTION_REFUSED);
         throw error;

       } else {

         return this.handleError('Get tasks', []);
         // return throwError(error);
       }

     })

   );

 }
我的方法是循环,但不知道如何返回:
注意:稍后,应该可以添加更多端点

const list = [0, 1];

  for (const i in list) {
    const endpoint = `${this.processEngineUriPrefix}` + i + `/runtime/tasks`;

    this.httpClient.get<TaskListModel>(endpoint, this.getHttpOptions(params)).pipe(
      tap(() => {

        this.logger.info('TasksService: getTasks() completed');

      }),
      catchError(error => {

        this.logger.info('TasksService: getTasks() -> catchError()');

        if (error === undefined) {

          error = new Error(HTTP_SERVER_ERROR_CONNECTION_REFUSED);
          throw error;

        } else {

          return this.handleError('Get tasks', []);
          // return throwError(error);
        }

      }))
  };
  return _________;

}
const list=[0,1];
用于(列表中的常数i){
const endpoint=`${this.processEngineUriPrefix}`+i+`/runtime/tasks`;
this.httpClient.get(端点,this.getHttpOptions(params)).pipe(
点击(()=>{
this.logger.info('TasksService:getTasks()已完成');
}),
catchError(错误=>{
this.logger.info('TasksService:getTasks()->catchError()');
如果(错误===未定义){
错误=新错误(HTTP\u服务器\u错误\u连接\u被拒绝);
投掷误差;
}否则{
返回此.handleError('Get tasks',[]);
//返回投掷器(错误);
}
}))
};
返回;
}
我注意到了这篇文章,但由于我对整个主题都很陌生,我不知道这是否是一种适合我的案例的方法,我无法为我的案例定制它

您可以使用RxJS运算符等待
for..of
循环完成,然后返回所有可观测值

首先,我们将HTTP请求重构为它自己的方法

getTask(i) {
  const endpoint = `${this.processEngineUriPrefix}` + i + `/runtime/tasks`;

  return this.httpClient.get<TaskListModel>(endpoint, this.getHttpOptions(params))
    .pipe(
      tap(() => {

        this.logger.info('TasksService: getTasks() completed');

      }),
      catchError(error => {

        this.logger.info('TasksService: getTasks() -> catchError()');

        if (error === undefined) {

          error = new Error(HTTP_SERVER_ERROR_CONNECTION_REFUSED);
          throw error;

        } else {

          return this.handleError('Get tasks', []);
          // return throwError(error);
        }

  }))

}

谢谢你的回复。将HTTP请求重构为自己的方法是很好的。但是我无法订阅observablesList,我必须在之前声明它吗常量列表=[0,1];常量observesList=[];对于(列表的常数i){observestList.push(this.getTask(i));}observestList.subscribe(res=>{//在这里处理其余部分});`此外,我还尝试了以下方法:```返回this.getTaskHelper(api++).pipe(expand(res=>{return of(1+api);}),take(2));```并获取错误…@Crobrg您是否声明了observablesList?
getTask(i) {
  const endpoint = `${this.processEngineUriPrefix}` + i + `/runtime/tasks`;

  return this.httpClient.get<TaskListModel>(endpoint, this.getHttpOptions(params))
    .pipe(
      tap(() => {

        this.logger.info('TasksService: getTasks() completed');

      }),
      catchError(error => {

        this.logger.info('TasksService: getTasks() -> catchError()');

        if (error === undefined) {

          error = new Error(HTTP_SERVER_ERROR_CONNECTION_REFUSED);
          throw error;

        } else {

          return this.handleError('Get tasks', []);
          // return throwError(error);
        }

  }))

}
const list = [0, 1];
const observablesList = [];

for (const i of list) {
  observablesList.push(this.getTask(i));
}  

observablesList.subscribe(res => {
  // handle the rest here
});