Search code examples
angularngrxngrx-store

NgRx 8 - Detected unserializable state for class


I know NgRx loves plain objects in the state, but why not this kind of one?

export class EventRecord {
  Id = -1;
  Title = "";

  private occurred?: string = undefined;
  get OccurredDate(): string {
    return this.occurred ? this.occurred : "";
  }
  set OccurredDate(value: string) {
    const mValues = moment(value);
    if (mValues.isValid()) {
      this.occurred = mValues.toISOString();
    } else {
      throw Error("Invalid date value");
    }
  }

  PictureUri?: string;
}

what's not serializable here?


Solution

  • I think the problem is that you can't serialize a class that also has behaviour attached (the getter and setter). You can only serialize pure data class (classes without behaviour, with only plain properties).