Search code examples
javascriptgojs

GoJS - Uncaught Error: Model.makeNodeDataKeyUnique failed on [object Object]


I am trying to set nodeKeyProperty of gojs diagram model. Following code is what I am trying to do -

<!DOCTYPE html>
<html>
<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/gojs/1.6.7/go-debug.js"></script>
</head>
<body>
    <div id="myDiagramDiv" style="width:400px; height:150px; background-color: #DAE4E4;"></div>
    <script type="text/javascript">
        var $ = go.GraphObject.make;
        var diagram = $(go.Diagram, "myDiagramDiv");

        diagram.nodeTemplate = $(go.Node, "Auto",
            $(go.Shape, "RoundedRectangle", new go.Binding("fill", "fill").makeTwoWay()),
            $(go.TextBlock, new go.Binding("text", "id"))
        );

        diagram.model = new go.GraphLinksModel();

        diagram.model.nodeKeyProperty = function(nodeData, id) {
            // id && (nodeData.id = id);// enabling it will fix the issue.
            return nodeData.id;
        };

        diagram.model.addNodeData({
            fill:"green"
        })
    </script>
</body>
</html>

But I am not able to run this code successfully. I am getting the following error -

    Uncaught Error: Model.makeNodeDataKeyUnique failed on [object Object].  Data not added to Model.
    at Object.k (go-debug.js:31)
    at X.L.addNodeData.L.vl (go-debug.js:356)
    at index-copy.html:26

I tried to debug the gojs code and somehow figured out that adding id && (nodeData.id = id) to nodeKeyProperty implementation fixes the issue. However, from the docs its not clear to me if it was required. Am I doing anything wrong?

Thanks in Advance.


Solution

  • That Model.nodeKeyProperty's documentation is incomplete. If the value is a function, it should take one or two arguments. If there is only one argument, it should return the number or string key value for that data Object. If there are two arguments, it should modify that Object to use the second argument value as its key value.

    But should you not just be setting Model.nodeKeyProperty to "id"?