antvis / XFlow

React component for building interactive diagrams.
https://x6.antv.antgroup.com/xflow/guide/introduction
MIT License
564 stars 233 forks source link

[xflow2.0] 更新画布时( update(id,data) ),data中含有 undefined,报错:Unexpected token u in JSON at position 0 #450

Closed jeff-nm closed 5 months ago

jeff-nm commented 5 months ago

Describe the bug

使用 useGraphStore() 提供的 updateNode 方法更新节点数据,data 参数中含有 { value: undefined } 类似的属性值,出现错误; 报错信息如下:

Uncaught SyntaxError: Unexpected token u in JSON at position 0
    at JSON.parse (<anonymous>)
    at a (<anonymous>:8:17)
    at <anonymous>:33:25
    at Array.forEach (<anonymous>)
    at a (<anonymous>:28:26)
    at <anonymous>:33:25
    at Array.forEach (<anonymous>)
    at a (<anonymous>:28:26)
    at <anonymous>:1:1

Your Example Website or App

示例在步骤中提供

Steps to Reproduce the Bug or Issue

  1. 调用 updateNode 方法,进入到 @antv/xflow/src/util/object.ts 中的 function apply(target: any, patchItem: any): any 方法;
  2. 单独测试 apply ,出现 **Uncaught SyntaxError: Unexpected token u in JSON at position 0**

    
    function apply(target: any, patchItem: any): any {
    /**
    * If the patch is anything other than an object,
    * the result will always be to replace
    * the entire target with the entire patch.
    */
    if (typeof patchItem !== "object" || Array.isArray(patchItem) || !patchItem) {
    return JSON.parse(JSON.stringify(patchItem)); //return new instance of variable
    }
    
    if (
    typeof patchItem === "object" &&
    patchItem.toJSON !== undefined &&
    typeof patchItem.toJSON === "function"
    ) {
    return patchItem.toJSON();
    }
    /** Also, it is not possible to
    * patch part of a target that is not an object,
    * such as to replace just some of the values in an array.
    */
    let targetResult = target;
    if (typeof target !== "object") {
    //Target is empty/not an object, so basically becomes patch, minus any null valued sections (becomes {} + patch)
    targetResult = { ...patchItem };
    }
    
    Object.keys(patchItem).forEach((k) => {
    if (!targetResult.hasOwnProperty(k)) targetResult[k] = patchItem[k]; //This ensure the key exists and TS can't throw a wobbly over an undefined key
    if (patchItem[k] === null) {
      delete targetResult[k];
    } else {
      targetResult[k] = apply(targetResult[k], patchItem[k]);
    }
    });
    return targetResult;
    }

apply({}, { data: undefined });



### Expected behavior

方法中仅对 `null` 的情形进行了 `delete targetResult[k];`,忽略了 `undefined` 的情形;
期望:对 `undefined` 进行处理

### Screenshots or Videos

_No response_

### Platform

- Browser: Chrome

### Additional context

_No response_
xflow-bot[bot] commented 5 months ago

👋 @jeff-nm

Thanks for opening your first issue here! If you're reporting a 🐞 bug, please make sure you include steps to reproduce it.

To help make it easier for us to investigate your issue, please follow the contributing guidelines.

We get a lot of issues on this repo, so please be patient and we will get back to you as soon as we can.

NewByVector commented 5 months ago

更新到 2.0.4.