简体   繁体   English

Angular - 观察者地图未按预期工作

[英]Angular - Observer map not working as intended

I'm trying to fetch data from my backend, creating a class object for each item I get我试图从我的后端获取数据,为我得到的每个项目创建一个类对象

 getRankingList(type: RankingListType, page: number) { let params = new HttpParams().set("pid", String(page)).set("limit", String(5)); return this.http.get(`http://127.0.0.1:3333/ranking/player/all`, { params }) .pipe( map(item => new RankingGuild(item['guild'], item['name'], item['country'], item['honor'], item['RawKey'])) ); }

The data I'm receiving from the backend looks like this:我从后端收到的数据如下所示:

[
  {
    "RawKey": "1",
    "honor": 0,
    "guild": "Test",
    "name": "test",
    "country": 1
  },
 {
    "RawKey": "2",
    "honor": 0,
    "guild": "Test2",
    "name": "test2",
    "country": 1
  }
]

But instead of iterating through the object, "item" is the object itself, meaning there is only one iteration returning the object that I had in the first place, rather than its entries.但不是遍历对象,“项目”是对象本身,这意味着只有一次迭代返回我首先拥有的对象,而不是它的条目。 I've been searching for hours to find a solution, but it seems like this is the correct way to do it, not sure why it doesn't work.我一直在寻找解决方案几个小时,但似乎这是正确的方法,不知道为什么它不起作用。

This is because the RxJS map operator and JavaScript's Array.map() are 2 different things altogether.这是因为 RxJS地图操作符和 JavaScript 的Array.map()完全是两个不同的东西。 You should read up on their differences.您应该仔细阅读它们的差异。

In short, the RxJS map operator allows you to apply a given project function to each value emitted by the source Observable, and emit the resulting values as an Observable.简而言之,RxJS map运算符允许您将给定的项目函数应用于源 Observable 发出的每个值,并将结果值作为 Observable 发出。 On the other hand, the Array.map() method merely creates a new array with the results of calling a provided function on every element in the calling array.另一方面, Array.map()方法仅创建一个新数组,其结果是在调用数组中的每个元素上调用提供的函数。

If you want to map the value returned by the response from the HTTP request, I believe this is what you should be doing instead.如果你想映射 HTTP 请求的响应返回的值,我相信这是你应该做的。

getRankingList(type: RankingListType, page: number) {

  let params = new HttpParams().set("pid", String(page)).set("limit", String(5));

  return this.http.get(`http://127.0.0.1:3333/ranking/player/all`, { params })
    .pipe(
      map(response => response.map(item => new RankingGuild(item['guild'], item['name'], item['country'], item['honor'], item['RawKey'])))
    );
}

Then, on your component itself, you may subscribe to the method to return the actual values itself.然后,在您的组件本身上,您可以订阅该方法以返回实际值本身。

getRankingList.subscribe(res => {
  // do the rest
})

The rx map operator is not the array map operator. rx map运算符不是数组map运算符。 The array map transforms items in an array, the rx map transforms items in a stream, and the item in the stream in this case is an array.数组map转换数组中的项目,rx map转换流中的项目,在这种情况下,流中的项目是一个数组。 Do this:做这个:

return this.http.get(`http://127.0.0.1:3333/ranking/player/all`, { params })
  .pipe(
    map(items => items.map(item => new RankingGuild(item['guild'], item['name'], item['country'], item['honor'], item['RawKey'])))
  );

Use the array map inside your rx map .使用阵列map内的RX map

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

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