d3.js: "Cannot read property 'weight' of undefined" when manually defining both nodes and links for force layout

NullVoxPopuli picture NullVoxPopuli · Jul 6, 2012 · Viewed 18.6k times · Source

I tried setting both nodes and links at the same time this way:

var force = d3.layout.force()
    .size([w, h])
    .nodes(nodes)
    .links(connections)
    .start();

nodes = [{"name":"data_base_id", "kind":"subgenre"},...]
connections = [{"source":"name_of_node", "target":"name_of_other_node"},...]

I have data that may not have connections, so it is necessary to defined the nodes, so that all of the nodes get rendered. And defining the genres is pretty easy. but I get this error;

Cannot read property 'weight' of undefined

And when I comment out .links(connections) the graph renders (juts a bunch of dots scattered throughout...) How do I get the connections / links to cooperate with d3?

I was reading the docs, and apparently the source and target have to be INDEXES of the nodes in the nodes array. Is there anyway to change that? So, I can use the name of a node rather than the index it has in an array?

Answer

air_bob picture air_bob · Sep 26, 2013

I encounter same problem before, it is due to there is null values in source/target of links. print out nodes and links information might help to debug