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 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