title: D3 force-directed graph nodes have no attribute "weight"
body: <p>I am using tweaked code from <a href="http://hughsk.github.io/colony/" rel="nofollow">http://hughsk.github.io/colony/</a> but I keep getting the error: </p>
  
  <blockquote>
    <p>Uncaught TypeError: Cannot read property 'weight' of undefined </p>
  </blockquote>
  
  <p>This stems from the force.nodes(nodes) function, for which the document says no weight needs to be set as defaults will be initialized by the layout when start() is created.</p>
  
  <p>Can someone see what I'm doing wrong that keeps the weight values from being initialized?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>var colony = {
    "nodes":[
      {
        "pages":123,
        "name":"Test",
        "id":2
      },
      {
        "pages":456,
        "name":"Test2",
        "id":3
      }
    ],
    "links":[
      {
        "source":123,
        "target":456,
        "weight":100
      }
    ]
  }
  
  var nodes = colony.nodes
    , links = colony.links
    , scale = 1
    , focus
  
  var width = 960
    , height = 960
    , link
    , node
    , text
    , textTarget = false
  
  var colors = {
        links: 'FAFAFA'
      , text: {
          subtitle: 'FAFAFA'
      }
      , nodes: {
          method: function(d) {
              return groups[d.group].color
          }
          , hover: 'FAFAFA'
          , dep: '252929'
      }
  }
  
  links.forEach(function(link) {
      var source = function(nodes,link){
          for (var i = 0; i &lt; nodes.length; i++){
              if (nodes[i].id == link.source){
                  return nodes[i];
              }
          }
      }
        , target = function(nodes,link){
          for (var i = 0; i &lt; nodes.length; i++){
              if (nodes[i].id == link.target){
                  return nodes[i];
              }
          }
      }
  
      source.children = source.children || []
      source.children.push(link.target)
  
      target.parents = target.parents || []
      target.parents.push(link.source)
  })
  
  var groups = nodes.reduce(function(groups, file) {
      var group = file.mgroup || 'none'
        , index = groups.indexOf(group)
  
      if (index === -1) {
          index = groups.length
          groups.push(group)
      }
  
      file.group = index
  
      return groups
  }, [])
  
  groups = groups.map(function(name, n) {
      var color = d3.hsl(n / groups.length * 300, 0.7, 0.725)
  
      return {
            name: name
          , color: color.toString()
      };
  })
  
  var force = d3.layout.force()
      .size([width, height])
      .charge(-50 * scale)
      .linkDistance(20 * scale)
      .on('tick', function() {
          link.attr('x1', function(d) { return d.source.x; })
              .attr('y1', function(d) { return d.source.y; })
              .attr('x2', function(d) { return d.target.x; })
              .attr('y2', function(d) { return d.target.y; })
  
          node.attr('cx', function(d) { return d.x; })
              .attr('cy', function(d) { return d.y; })
  
          if (textTarget) {
              text.attr('transform'
                      , 'translate(' + textTarget.x + ',' + textTarget.y + ')')
          }
      })
  
  var vis = d3.select('body')
      .append('svg')
      .attr('width', width)
      .attr('height', height)
  
  force.nodes(nodes)
       .links(links)
       .start()
  </code></pre>