javascript - Ngrx Angular - 在简单对象上检测到不可序列化的 Action

标签 javascript angular typescript ngrx

我不明白为什么 ngrx 在我试图向我的 api 发送一个简单的对象时会弹出这个错误,你能给我一些关于 ngrx 的建议以及它拒绝序列化我的对象的原因吗?

我试着把 strictActionSerializabilityfalse ,没有错误,但没有对象发送到我的 api...

错误:

Error: Detected unserializable action at "createdPath"



我如何称呼我的行动:
   this.storePath.dispatch(PathActions.createPath({ createdPath }));

在 actions.ts 文件中:
export const createPath = createAction('[BOT/GROUP] CREATE PATH', props<{ createdPath: Path }>());

而我的效果:
createPath$ = createEffect(() =>
this.actions$.pipe(
  ofType(PathActions.createPath),
  map(action => action.createdPath),
  exhaustMap((createdPath: Path) =>
    this.pathService.createPath(createdPath).pipe(
      map(createdPath => PathActions.createPathSuccess({ createdPath })),
      catchError(error => of(PathActions.createPathFailure({ error }))))
  )
 )
);

我的对象作为 JSON 发送:
{
"monsterLevel": [],
"monsterQuantity": [],
"monsterCapture": [],
"pathAction": [
    {
        "actions": [
            {
                "order": 1,
                "fightAction": {
                    "isAlone": false
                }
            },
            {
                "order": 2,
                "moveAction": {
                    "direction": [
                        "Right",
                        "Bottom"
                    ],
                    "toGoBank": false,
                    "toBackBank": false
                }
            }
        ],
        "mapPos": "-14;-53"
    },
    {
        "actions": [
            {
                "order": 1,
                "fightAction": {
                    "isAlone": false
                }
            },
            {
                "order": 2,
                "moveAction": {
                    "direction": [
                        "Top",
                        "Right"
                    ],
                    "toGoBank": false,
                    "toBackBank": false
                }
            }
        ],
        "mapPos": "-14;-52"
    },
    {
        "actions": [
            {
                "order": 1,
                "fightAction": {
                    "isAlone": false
                }
            },
            {
                "order": 2,
                "moveAction": {
                    "direction": [
                        "Top",
                        "Left"
                    ],
                    "toGoBank": false,
                    "toBackBank": false
                }
            }
        ],
        "mapPos": "-13;-52"
    },
    {
        "actions": [
            {
                "order": 1,
                "fightAction": {
                    "isAlone": false
                }
            },
            {
                "order": 2,
                "moveAction": {
                    "direction": [
                        "Left",
                        "Bottom"
                    ],
                    "toGoBank": false,
                    "toBackBank": false
                }
            }
        ],
        "mapPos": "-13;-53"
    },
    {
        "actions": [
            {
                "order": 1,
                "moveAction": {
                    "direction": [
                        "Bottom"
                    ],
                    "toGoBank": true,
                    "toBackBank": false
                }
            }
        ],
        "mapPos": "-14;-51"
    },
    {
        "actions": [
            {
                "order": 1,
                "moveAction": {
                    "direction": [
                        "Bottom"
                    ],
                    "toGoBank": true,
                    "toBackBank": false
                }
            }
        ],
        "mapPos": "-14;-50"
    },
    {
        "actions": [
            {
                "order": 1,
                "moveAction": {
                    "direction": [
                        "Bottom"
                    ],
                    "toGoBank": true,
                    "toBackBank": false
                }
            }
        ],
        "mapPos": "-14;-49"
    },
    {
        "actions": [
            {
                "order": 1,
                "moveAction": {
                    "direction": [
                        "Bottom"
                    ],
                    "toGoBank": true,
                    "toBackBank": false
                }
            }
        ],
        "mapPos": "-14;-48"
    },
    {
        "actions": [
            {
                "order": 1,
                "moveAction": {
                    "cellId": 150,
                    "toGoBank": true,
                    "toBackBank": false
                }
            },
            {
                "order": 2,
                "zaapAction": {
                    "destination": "-32,-58",
                    "zaapId": 1,
                    "toBackBank": false,
                    "toGoBank": true
                }
            }
        ],
        "mapPos": "-14;-47"
    }
],
"name": "feef",
"type": 0,
"monsterQuantityMin": 0,
"monsterQuantityMax": 8,
"groupLevelMin": 0,
"groupLevelMax": 999,
"maxPod": 51,
"leaderBank": true
}

使用的类:
export class Path {
  name: string;
  type: number; /* 0 fight , 1 gather */
  maxPod: number=80;
  monsterQuantityMin: number =0;
  monsterQuantityMax: number =8;
  groupLevelMin: number =0;
  groupLevelMax: number=9999;
  isCapture: boolean =false;
  leaderBank: boolean = false;
  captureItem: number;
  monsterLevel?: SpecificMonsterLevel[];
  monsterQuantity?: SpecificMonsterQuantity[];
  monsterCapture?: CaptureMonsterQuantity[];
  pathAction: PathAction[];
}

祝您有美好的一天,感谢您的帮助!

最佳答案

对于纯数据类对象,您可以使用

JSON.parse(JSON.stringify(product))

否则,我建议添加 toJSON()序列化方法(由 JSON.stringify 自动使用)
public class Foo{
    private _bar:string;

    constructor(){ this._bar='Baz'; }

    get bar():string{return this._bar}

    toJSON() {
      return {bar: _bar};
    }

    static fromJSON(json) {
      ...
    }
}

引用 - Angular 2 (or 4) object serialization

关于javascript - Ngrx Angular - 在简单对象上检测到不可序列化的 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60560545/

相关文章:

javascript - 防止表单提交重新加载页面

javascript - javascript项目的firebase初始化代码是否需要在每个网页上运行?

javascript - 在 Javascript 中更改日期时区?

javascript - 疯狂使用 Angular Directive(指令)和范围更新

node.js - 通过 Typescript 在 Node 流上使用 Array.reduce

javascript - 使用多数组通过 dc.js、d3 和 crossfilter 在散点图上绘制数据

javascript - 带有 javascript 和日期选择器的 Angular 2

Angular-Material:在表单标签上显示工具提示

node.js - GCS 设置更改后,Cloud Build 不断失败

typescript - 使用导出类型时为 "error TS1110: Type expected"