ERROR TypeError: Cannot read property 'authors' of undefined
Ловлю ошибку когда селекчу список авторов, Selector
import { createFeatureSelector, createSelector } from "@ngrx/store";
import { AuthorModel } from "../../models/author.interface";
import { reducer } from "../reducers/author.reducer";
import { LIST_AUTHOR_REDUCER } from "../reducers/list-authors.reducer";
import { AuthorState } from "../state/author.state";
const selectGetAuthor = createFeatureSelector<AuthorState>(reducer);
export const listAuthors = createSelector(
selectGetAuthor,
(state) => state.listAuthorState.authors
);
component в котором делаю селект
import { Component, OnInit } from '@angular/core';
import { ListAuthorsState } from '../../author/store/state/list-authors.state';
import { EditPrintingEdition } from '../models/edit-printing-edition.interface';
import { CurrencyTypeEnum } from '../models/enums/printing-edition-enum/currency-type';
import { PrintingTypeEnum } from '../models/enums/printing-edition-enum/printing-type';
import { UpdatePrintingEdition } from '../store/actions/update-printing-edition.action';
import { AdminState } from '../store/state/admin.state';
@Component({
selector: 'app-update-printing-edition',
templateUrl: './update-printing-edition.component.html',
styleUrls: ['./update-printing-edition.component.scss']
})
export class UpdatePrintingEditionComponent implements OnInit {
updatePrintingEditionForm: FormGroup
errorMessage: any;
authors: AuthorModel[];
editionType: string[];
currencyType: string[];
id: string;
constructor(
private activateRoute: ActivatedRoute,
private store$: Store<AppState>
)
{
this.updatePrintingEditionForm = new FormGroup({
"title" : new FormControl(""),
"description" : new FormControl(""),
"price" : new FormControl(""),
"currency" : new FormControl(""),
"type" : new FormControl(""),
"authors": new FormControl("")
});
}
ngOnInit(): void {
this.store$.dispatch(new ListAuthors());
this.store$.pipe(select(listAuthors)).subscribe( <--- Тут ошибка
data => {
this.authors = data;
}
)
this.editionType = Object.keys(PrintingTypeEnum).filter(Number);
this.currencyType = Object.keys(CurrencyTypeEnum).filter(Number);
this.id = this.activateRoute.snapshot.paramMap.get('id');
}
updatePrintingEdition(){
let form: EditPrintingEdition = {
id: Number(this.id),
authorsId: this.updatePrintingEditionForm.controls['authors'].value,
currency: this.updatePrintingEditionForm.controls['currency'].value,
description: this.updatePrintingEditionForm.controls['description'].value,
price: this.updatePrintingEditionForm.controls['price'].value,
printing: this.updatePrintingEditionForm.controls['type'].value,
title: this.updatePrintingEditionForm.controls['title'].value
}
console.log(this.updatePrintingEditionForm);
this.store$.dispatch(new UpdatePrintingEdition(form));
}
}
Сама ошибка
ERROR TypeError: Cannot read property 'authors' of undefined
at list-authors.selector.ts:12
at ngrx-store.js:1096
at memoized (ngrx-store.js:1011)
at defaultStateFn (ngrx-store.js:1051)
at ngrx-store.js:1105
at memoized (ngrx-store.js:1011)
at MapSubscriber.project (ngrx-store.js:868)
at MapSubscriber._next (map.js:29)
at MapSubscriber.next (Subscriber.js:49)
at MapSubscriber._next (map.js:35)
ListSelectState
import { AuthorModel } from "../../models/author.interface";
export interface ListAuthorsState{
authors: AuthorModel[];
}
export const initialListAuthorsState: ListAuthorsState = {
authors: null
}
reducer
import { ListAuthorsState, initialListAuthorsState } from '../state/list-authors.state';
import { AdminState } from 'src/app/pages/admin/store/state/admin.state';
import { AuthorModel } from '../../models/author.interface';
import { EListAuthors, ListAuthorsActions } from '../actions/list-authors.action';
export const LIST_AUTHOR_REDUCER = "author";
export const listAuthorsReducer = (
state = initialListAuthorsState,
action: ListAuthorsActions
): ListAuthorsState => {
switch (action.type){
case EListAuthors.ListAuthors: {
return {
...state
}
}
case EListAuthors.ListAuthorsSuccess: {
return {
...state,
authors: action.payload
};
}
default: {
return {
...state
};
}
}
}