Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jeff-nm opened this issue Jan 19, 2024 · 3 comments
Labels
bug Something isn't working.

Comments

@jeff-nm
Copy link

jeff-nm commented Jan 19, 2024

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
Copy link
Contributor

xflow-bot bot commented Jan 19, 2024

👋 @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 NewByVector added the bug Something isn't working. label Jan 24, 2024
@NewByVector
Copy link
Contributor

更新到 2.0.4.

@xflow-bot
Copy link
Contributor

xflow-bot bot commented Jan 24, 2025

This thread has been automatically locked because it has not had recent activity.

Please open a new issue for related bugs and link to relevant comments in this thread.

@xflow-bot xflow-bot bot locked as resolved and limited conversation to collaborators Jan 24, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working.
Projects
None yet
Development

No branches or pull requests

2 participants