Andrey
Andrey

Reputation: 73

'd3.nest()' cannot read property of undefined

I'm trying to use very simple d3.nest() functionality but get failed with the error 'Cannot read property of undefined' related to one of the data columns.

First I tried to process the stand alone txt file and got stuck. Second I tried to process a simple variable created inside the code and didn't succeeded.

I understand that the problem I'm posting could have been discussed not once. But I searched and didn't find any responses just for my case. Think I'm doing incorrectly something that is peculiar for a hole newbie only.

Here's the code:

const db = [
        {
            'net': '36,6',
            'lon': '30',
            'lat': '50'
        },
        {
            'net': 'erka',
            'lon': '40',
            'lat': '55'
        },
        {
            'net': 'erka',
            'lon': '40',
            'lat': '70'
        }
        ];

    console.log(db); //output looks fine with all three columns needed 

    const nest = d3.nest(db)
        .key(function(d) {return d.net;}) //triggers the error
        .entries(function(d) {return d.lon;});

The error is:

Cannot read property 'net' of undefined

Upvotes: 2

Views: 316

Answers (1)

Gerardo Furtado
Gerardo Furtado

Reputation: 102174

The nest() method doesn't ask for any argument. Therefore, this...

d3.nest(db)
//-------^

... has no effect. Instead of that, you have to pass your data array to entries. Have a look:

const db = [{
    'net': '36,6',
    'lon': '30',
    'lat': '50'
  },
  {
    'net': 'erka',
    'lon': '40',
    'lat': '55'
  },
  {
    'net': 'erka',
    'lon': '40',
    'lat': '70'
  }
];

const nest = d3.nest()
  .key(function(d) {
    return d.net;
  })
  .entries(db);

console.log(nest)
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>

It's not clear to me why you're passing that anonymous function to entries. I'd say that you probably want another key.

Upvotes: 2

Related Questions