简体   繁体   English

Angular 7 将数据添加到 http 请求的结果

[英]Angular 7 adding data to result of http request

I get an array of IDs (assetIDs) and using those IDs I want to ask for data.我得到了一组 ID(资产 ID),并使用这些 ID 来获取数据。 For each http request I'm receiving one or more datasets.对于每个 http 请求,我都会收到一个或多个数据集。 I want to add the request ID to each dataset and then return the data.我想将请求 ID 添加到每个数据集,然后返回数据。

Getting and returning the data works just fine, but I don't know how to add that assetID to the dataset.获取和返回数据工作正常,但我不知道如何将该资产 ID 添加到数据集。

When I do it like in the following code snippet, I only get the first dataset of each ID.当我像下面的代码片段那样做时,我只得到每个 ID 的第一个数据集。 (Of course...because of the [0]). (当然......因为[0])。 But how can I iterate over all datasets?但是如何迭代所有数据集?

getData(assetIds: Array<string>): Observable<any> {
  const data = assetIds.map(assetId => {
    // for each assetId
    const path = this.serverUrl + '?' + 'assetid=' + assetId;
    return this.httpClient.get(path).pipe(
      map((res: any[]) => {
        return {
          name: res[0].name,
          type: res[0].type,
          asset: assetId
        };
    }));
});

// return combined result of each assetId request
return forkJoin(data);
}

I also tried the following, but I don't get any data when doing this:我还尝试了以下操作,但是在执行此操作时我没有得到任何数据:

getData(assetIds: Array<string>): Observable<any> {
 const data = assetIds.map(assetId => {
  // for each assetId
  const path = this.serverUrl + '?' + 'assetid=' + assetId;
  return this.httpClient.get(path).pipe(
    map((res: any[]) => {
      const resultArray = [];
      res.forEach(element => {
        const row = {
          name: res[element].name,
          type: res[element].type,
          asset: assetId
        };
        resultArray.push(row);
      });
      return resultArray;
    }));
});
// return combined result of each assetId request
return forkJoin(data);

} }

Your second aproach seems fine.你的第二个方法似乎很好。 I believe the problem is that you are using the rxjs operator forkJoin .我相信问题在于您使用的是rxjs运算符forkJoin

As RXJS docs say, this operator emits value when正如 RXJS 文档所说,当

When all observables complete, emit the last emitted value from each.当所有 observable 完成后,从每个 observable 发出最后发出的值。

You basically have 2 options, change operator forkJoin to zip您基本上有 2 个选项,将操作符forkJoin更改为zip

After all observables emit, emit values as an array在所有 observable 发出后,将值作为数组发出

Or add the take(1) operator after the map on pipe .或者在pipe上的map之后添加take(1)运算符。 Take operator will complete the observable after 1 value is emmited, permitting forkJoin to emit its values Take 操作符将在发出 1 个值后完成 observable,允许forkJoin发出它的值

You can use map also on your result array.您也可以在结果数组上使用map Something like this:像这样的东西:

const data = assetIds.map(assetId => {
    // for each assetId
    const path = this.serverUrl + '?' + 'assetid=' + assetId;
    return this.httpClient.get(path).pipe(
      map((res: any[]) => res.map(item => {
        return {
          name: item.name,
          type: item.type,
          asset: assetId
        }
      })));
  });

  // return combined result of each assetId request
  return forkJoin(data);
}

Thanks for your replies.感谢您的回复。 I tried everything but I guess the problem was that I used "element" as index within the array.我尝试了所有方法,但我想问题是我使用“元素”作为数组中的索引。 The following code now works just fine:下面的代码现在工作得很好:

return this.httpClient.get(path)
  .pipe(
    map((datasets: AssetFilesTableItem[]) => {
      const result: AssetFilesTableItem[] = [];
      let i = 0;
      datasets.forEach(element => {
        const dataset: AssetFilesTableItem = {
          name: datasets[i].name,
          type: datasets[i].type,
          size: datasets[i].size,
          timestamp: datasets[i].timestamp,
          created: datasets[i].created,
          updated: datasets[i].updated,
          asset: assetId
        };
        result.push(dataset);
        i++;
      });

      return result;
    }));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM