A better way to report the bug about [ 'id' reduplicated in data ] #4816
zqqcee posted onGitHub
Describe the bug
For the id
attribute of nodes and edges in the data, there may some errors are as follow:
1 - When a node and a edge have the same id
in the data, there'll be a bug
eg:
const data = {
nodes: [
{
id: 1, // same
data: {
x: 100,
y: 100,
type: 'star-node',
},
},
{
id: 2,
data: {
x: 200,
y: 100,
type: 'rect-node',
},
},
],
edges: [
{
id:1, // same
source: 1,
target: 2,
data: {},
},
],
};
<img width="713" alt="image" src="https://github.com/antvis/G6/assets/55946653/f3e1dbc9-0633-410c-a262-35dc1b22d6ae">
The error msg is
TypeError: Cannot read properties of undefined (reading 'getType')
The reason for this is that doAddNode
and doAddEdge
which come from @antv/graphlib/src/graph.ts
adding node and edge into the same object.
Therefore, the edge (id is 1) replaced the node (id is 1) because of the same key, and the itemMap
from ItemController cannot find the node (id is 1).
There're two ways to fix it, in my opinion.
- fix it in the
@antv/graphlib/src/graph.ts
, divided the nodes and edges into two different objects. - throw a more appropriate error message in the
graph.ts > doAddEdge
, like "the id '1' is reduplicated".
2- When some nodes or edges (more than one) haven't id
field , there'll be a bug.
eg:
const data = {
nodes: [
{
data: {
x: 100,
y: 100,
type: 'star-node',
},
},
{
data: {
x: 200,
y: 100,
type: 'rect-node',
},
},
],
edges: [
{
id:1,
source: 1,
target: 2,
data: {},
},
],
};
<img width="595" alt="image" src="https://github.com/antvis/G6/assets/55946653/427ce9e7-620b-4df7-aa01-b3a7b04b15f8">
The error msg is
Error: Node already exists: undefined
I guess the error message "The id field is required" would be better :)
Your Example Website or App
http://127.0.0.1:8080/?name=rect
Steps to Reproduce the Bug or Issue
- Go to the v5 display platform
- Visit the demo:
rect.ts
- Modify the data as the example above
Expected behavior
More appropriate error message would be better
Screenshots or Videos
No response
Platform
- OS: [e.g. macOS, Windows, Linux]
- Browser: [e.g. Chrome, Safari, Firefox]
- Version: [e.g. 91.1]
Additional context
No response