首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Reducer在ngrx中抛出“error object null is not iterable”

Reducer在ngrx中抛出“error object null is not iterable”
EN

Stack Overflow用户
提问于 2020-02-18 15:29:33
回答 1查看 246关注 0票数 0

我有一个名为doctors.list.component.ts的组件,它呈现一个医生列表。我使用@ngrx/effect从API获得了这个医生。我想在这个列表中使用ng-modal添加一个新的医生。流程是,当用户选择医生表单ng-modal时,必须将其添加到现有的医生列表中。为此,我编写了新的action和reducer。这个reducer抛出error object null is not iterable

这是我的doctorlist.action.ts

代码语言:javascript
运行
复制
import { createAction, props } from '@ngrx/store';
import { Doctors } from 'src/app/admin/models/doctorProfileAdmin'

export const loadDoctors = createAction('[Doctors] Doctors List Request', props<{ hospitalId: string }>())

export const loadDoctorsSuccess = createAction('[Doctors] Doctors List Load Success', props<{ doctors: Doctors[] }>())

export const loadDoctorsFail = createAction('[Doctors] Doctors List Load Fail', props<{ errorMessage: string }>())

export const addExistingDoctorToList = createAction('[Doctors] Doctors List Load Success', props<{ doctor: Doctors }>())

在我的doctor.list.reducer.ts

代码语言:javascript
运行
复制
import { createReducer, on, Action } from '@ngrx/store';
import { DoctorListState } from '../state/doctorList.state';
import { loadDoctors, loadDoctorsSuccess, loadDoctorsFail,addExistingDoctorToList } from '../actions/doctorList.actions';


const initialState: DoctorListState = {
    doctors:null,
    error: null,
    isLoading: false
};

const doctorListReducer = createReducer(initialState,
    on(loadDoctors, (state) => {
        return { ...state, isLoading: true };
    }),
    on(loadDoctorsSuccess, (state, { doctors }) => {
        return { ...state, doctors: doctors, isLoading: false };
    }),
    on(loadDoctorsFail, ((state, { errorMessage }) => {
        return { ...state, errorMes: errorMessage, isLoading: false };
    })),
    on(addExistingDoctorToList, (state, { doctor }) => {
        return { ...state, doctors:[...state.doctors,doctor], isLoading: false };
    }),

);

export function reducer(state: DoctorListState | undefined, action: Action) {
    return doctorListReducer(state, action);
}

我也将我的initialState doctors属性更改为空数组.then此错误没有出现,但我的医生列表也没有显示在组件中

这就是我在组件初始化时如何让医生访问我的doctors.list.component.ts的方法。

代码语言:javascript
运行
复制
 this.store.pipe(select(selectDoctorsList)).pipe(skip(1)).subscribe(payload => {
        this.doctorList = payload;
 })

添加新的docotr事件单击函数

代码语言:javascript
运行
复制
addNewDoctor(){
   this.store.dispatch(addExistingDoctorToList({doctor:selectedDoctor}))
}

我不明白我在这里做错了什么。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-18 16:33:33

查看操作定义

代码语言:javascript
运行
复制
export const loadDoctorsSuccess = createAction('[Doctors] Doctors List Load Success', props<{ doctors: Doctors[] }>())

export const addExistingDoctorToList = createAction('[Doctors] Doctors List Load Success', props<{ doctor: Doctors }>())

这两个操作具有用作action.type的相同字符串。它们应该是唯一的,因此ngrx会将它们视为不同的操作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60275806

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档