Giter VIP home page Giter VIP logo

Comments (10)

DonRichards avatar DonRichards commented on June 14, 2024

I found a work around, it looks like you added [] for some reason.
data.subdivisions[0].names.en

from nodejs-geoip2ws.

fvdm avatar fvdm commented on June 14, 2024

What does console.log (data.subdivisions) give you?
My code does not alter the API response, it is provided exactly as received.

from nodejs-geoip2ws.

DonRichards avatar DonRichards commented on June 14, 2024

I listed the outputs above. Output is this.

[object Object]

from nodejs-geoip2ws.

oschwald avatar oschwald commented on June 14, 2024

Subdivisions is an array. There can be multiple levels of ISO subdivisions for some countries.

from nodejs-geoip2ws.

fvdm avatar fvdm commented on June 14, 2024

Indeed, subdivisions is an array or not available (undefined). When it is available the last element is usually the state, depending on region.

function getState (data) {
  return Array.isArray (data.subdivisions) && data.subdivisions.pop().names.en || null;
}

insights ('129.67.242.154', function (err, data) {
  if (err) { return console.log (err); }
  console.log ('State: %s', getState (data));
});

(edit: shorter code)

from nodejs-geoip2ws.

oschwald avatar oschwald commented on June 14, 2024

In many countries, such as the US, there is only one subdivision level, corresponding to states. However, some countries such as the United Kingdom have multiple levels, e.g., [0] England, [1] Oxfordshire. The subdivisions are always largest to smallest.

from nodejs-geoip2ws.

DonRichards avatar DonRichards commented on June 14, 2024

That makes using this kinda messy. If statements could be endless with this logic. I didn't notice the documentation stating this. Is there a more descriptive rundown on this somewhere?

from nodejs-geoip2ws.

fvdm avatar fvdm commented on June 14, 2024

Not that I know of. I'm affraid there is no easier solution as each region has their own system.

from nodejs-geoip2ws.

oschwald avatar oschwald commented on June 14, 2024

The MaxMind GeoIP2 JavaScript API adds in an empty array if it doesn't exist as well as adding a most_specific_subdivision property that returns the last element in the array. Maybe something similar could be done with this API.

from nodejs-geoip2ws.

fvdm avatar fvdm commented on June 14, 2024

Alright, it's on npm :)
Thanks for the hint @oschwald

from nodejs-geoip2ws.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.