2

I have a Heisenbug. With the following code:

    const node = graphBox
        .selectAll("g.node")
        .data(nodes, d => d.title)
        .join("g")
          .attr("class", "node");

    node.on("dblclick", d => {
        const refs =  d3.select(this)
            .selectAll("text.ref")
            .data(d.refs)
            .join("text");
        ...
    });

this is a g element. d.refs is populated with an array. When this code runs through on its own, refs comes up empty. It looks like this:

refs: Pt
    _groups: []
    _parents: []

When I stop a debugger just before the refs assignment, and run the identical code in console, it comes up with this:

refs: Pt 
    _groups: [Array(1)]
    _parents: [g]

I'm really stumped. What could cause this?

Gerardo Furtado
  • 100,839
  • 9
  • 121
  • 171
Laizer
  • 5,932
  • 7
  • 46
  • 73
  • @GerardoFurtado Giving it a second thought, the `undefined` makes me wonder if the closure was correct or, at least, if the targets are the right ones. Unless this runs on a server it should have logged `window` instead of `undefined`. This might even be a case of the notorious return-from-asnyc problem with the data not being available during a regular execution in contrast to it being loaded while halted in the debugger. – altocumulus Aug 20 '19 at 07:38
  • Could you please provide more context around your code, i.e. when and where the data is loaded and how this code segments are called? Try putting a `console.log(nodes)` before your above two statements. It would be helpful if you set up a [mcve]. – altocumulus Aug 20 '19 at 07:41
  • @altocumulus it can be `undefined` in the browser: https://jsfiddle.net/cemhyds5/. Anyway, feel free to change the dupe targets, reopen it, etc... – Gerardo Furtado Aug 20 '19 at 09:28
  • @GerardoFurtado You changed the rules ;-) Strict mode is a different ruleset and does not apply to arrow functions: https://jsfiddle.net/xa9Lopgu/ Still `window`. – altocumulus Aug 20 '19 at 09:42
  • @altocumulus you got me wrong: I said that a *possible* explanation is that all of OP's code is wrapped inside a strict IIFE: https://jsfiddle.net/wLknme41/. Still `undefined`. Your move. – Gerardo Furtado Aug 20 '19 at 09:51
  • @GerardoFurtado Ah, now I got you. Doesn't even need to be an IIFE, though, just strict mode. Now it's OP's move. – altocumulus Aug 20 '19 at 10:55

0 Answers0