我使用 Ngrx 的子集选择器不起作用

问题描述 投票:0回答:2

我正在使用 Angular 17 和 ngrx。

我有以下工作正常的选择器:

export const getPhotoState = createFeatureSelector<ProductResponse>('selectedPhotos');

export const selectPhotos = createSelector(getPhotoState, (state: ProductResponse) => { return state.results });

export const PhotoCount = createSelector(getPhotoState, (state: ProductResponse) => { return state.item_Count });

我不想编写另一个效果/减速器来发送到服务器,因为我只想获取已返回的数据的子集。

我写了以下内容:

export const XboxPhotos = createSelector(selectPhotos, (reducedPhotos) => {
  return xboxPhotos('xbox', reducedPhotos)
})

function xboxPhotos(key: string, array: any) {
  return array.reduce((all: Product[], current: Product) => {
    if (current.platform == key) {
      all.push(current);
    }
  }, []) as Product[]
}

我收到错误:

无法读取未定义的属性(读取“push”)

reducedPhotos 是产品[]

angular ngrx
2个回答
0
投票

返回操作后的值

function xboxPhotos(key: string, array: any) {
  return array.reduce((all: Product[], current: Product) => {
    if (current.platform == key) {
      all.push(current);
    }
    return all; // <- changed here
  }, []) as Product[]
}

0
投票

您正在修改累加器(在本例中为

all
数组),但从未返回它。如果您在减速器回调结束时
return all
,您将不会再看到此错误。

© www.soinside.com 2019 - 2024. All rights reserved.