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)
I think you might have null values in your source and target. I had this bug too and fixed it by filtering out the null values.