简体   繁体   English

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

[英]Ngrx Angular - Detected unserializable action at simple object

i don't get why ngrx pop this error while i'm trying to send to my api a simple object, could you give me some advice about ngrx and the reason why it refuse to serialize my object ?我不明白为什么 ngrx 在我试图向我的 api 发送一个简单的对象时会弹出这个错误,你能给我一些关于 ngrx 的建议以及它拒绝序列化我的对象的原因吗?

I tried to put strictActionSerializability to false , no error but no object sent to my api...我试图将strictActionSerializabilityfalse ,没有错误但没有对象发送到我的 api ......

Error :错误 :

Error: Detected unserializable action at "createdPath"错误:在“createdPath”处检测到不可序列化的操作

How i call my action :我如何称呼我的行动:

   this.storePath.dispatch(PathActions.createPath({ createdPath }));

In actions.ts file :在 actions.ts 文件中:

export const createPath = createAction('[BOT/GROUP] CREATE PATH', props<{ createdPath: Path }>());

And my effect :我的效果:

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 }))))
  )
 )
);

My object sent as JSON :我的对象作为 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
}

Class used:使用的类:

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[];
}

have a good day, and thanks for your help !祝您有美好的一天,感谢您的帮助!

For a pure data class object you can use对于纯数据类对象,您可以使用

JSON.parse(JSON.stringify(product))

Otherwise, I suggest adding a toJSON() serialization method (which is automatically used by JSON.stringify)否则,我建议添加一个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) {
      ...
    }
}

Reference - Angular 2 (or 4) object serialization参考 - Angular 2(或 4)对象序列化

@Andrew Allen 通过字符串化和重新解析我的对象解决了我的问题:

  this.storePath.dispatch(PathActions.createPath({ createdPath: JSON.parse(JSON.stringify(createdPath)) }));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 8 NgRx - 错误:检测到不可序列化的动作 - Angular 8 NgRx - Error: Detected unserializable action 如何存储一对键和值,并使用动作动态添加它们? 映射导致“检测到不可序列化状态”错误 - How to store a pairs of key and value, and add them dynamically using action? Map cause 'detected unserializable state' error 在嵌套的 object Angular 中更新 ngrx state - Update ngrx state in nested object Angular Angular 6 + Ngrx状态值更新,无需使用操作 - Angular 6 + Ngrx state value update without using action 如何在Angular NgRx效果中进行http调用之前调度动作? - How to dispatch an action before making an http call in Angular NgRx effects? Angular ngrx 属性“payload”在“Action”类型上不存在 - Angular ngrx Property 'payload' does not exist on type 'Action' 使用 angular 中的 NgRx 组件存储进行订阅时未检测到存储值更新更改 - Store value update change not detected on subscribe using NgRx component store in angular Angular | NgRx 检查 object 中的 null 字段,然后将其放入下拉列表 - Angular | NgRx Check for null field in an object before putting it in a list of dropdown 在 angular html 模板中显示来自ngrx 存储的单个 object - Displaying a single object from ngrx store in the angular html template ngRx 操作未正确分派 - ngRx action not dispatching correctly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM