2017-10-02 6 views
1

文字列の配列を入力として取得するメソッドがあります。(tagArray: string[])今度は、この配列をループし、タグがすでにデータベースに入っているかどうかを調べるためにサーバーに検索(POST経由)を実行します。配列を通した角度ループとforkJoinでオブザーバブルを収集

タグが存在する場合、対応するタグオブジェクトはサーバーによって返され、別の配列(searchFilter: { tags: Tag[] }[])に格納されます。

存在しない場合、サーバーにバックエンドに不足しているタグを格納するための別のPOST要求が行われ、その後、作成されたタグオブジェクトはsearchFilter['tags']に格納されます。私はforkJoinの観測を返すことができるように

performTagMatching(tagArray: string[]):any { 
    for(let tagElem of tagArray) { 
    // create search request for tag lookup 
    const searchRequest: SearchRequest = { term: tagElem, operation: 'exact'}; 

    this.apiClientService.findTagByTitle(searchRequest).subscribe(
     (response) => { 
     // Check tag search result for existence of tag 
     if(response.length == 0) { 
      // No tag found -> create tag and add to search tag list 
      this.searchFilter['tags'].push(this.saveNewTag(tagElem)); 
     } else { 
      // Tag found -> add to search tag list 
      this.searchFilter['tags'].push(response[0]); 
     } 
     }, 
     (error) => { 
     console.log(error); 
     } 
    ); 
    } 
} 

は、どのように私はforkJoinでこれを置くことができますか?

私はここに二つの問題があります。私は私が去った後、私はもうアクセスできません応答で「tagElem」を必要とする

  1. をforループ

  2. 私にはわかりません私は、メソッドの呼び出し側に配列への復帰を観測をプッシュすることができますどのように

答えて

1

forループからあなたのHTTP呼び出しを分離し、別の関数としてそれを呼び出します。

performTagMatching(tagArray: string[]): any { 
    for (let tagElem of tagArray) { 
     // create search request for tag lookup 
     const searchRequest: SearchRequest = { 
      term: tagElem, 
      operation: 'exact' 
     }; 

     this.callMactchFunc(tagElem) 
    } 
} 

private callMactchFunc(tagElem) { 
    this.apiClientService.findTagByTitle(searchRequest).subscribe(
     (response) => { 
      // Check tag search result for existence of tag 
      if (response.length == 0) { 
       // No tag found -> create tag and add to search tag list 
       this.searchFilter['tags'].push(this.saveNewTag(tagElem)); 
      } else { 
       // Tag found -> add to search tag list 
       this.searchFilter['tags'].push(response[0]); 
      } 
     }, 
     (error) => { 
      console.log(error); 
     } 
    ); 
} 
+0

しかし、私の問題は残っています。どのようにしてforkJoinにすべてのリクエストを入れ、後でforkJoinを購読するときにもtagElemにアクセスできますか? – user1337

関連する問題