Comments (10)
I found a work around, it looks like you added [] for some reason.
data.subdivisions[0].names.en
from nodejs-geoip2ws.
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.
I listed the outputs above. Output is this.
[object Object]
from nodejs-geoip2ws.
Subdivisions is an array. There can be multiple levels of ISO subdivisions for some countries.
from nodejs-geoip2ws.
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.
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.
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.
Not that I know of. I'm affraid there is no easier solution as each region has their own system.
from nodejs-geoip2ws.
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.
Alright, it's on npm :)
Thanks for the hint @oschwald
from nodejs-geoip2ws.
Related Issues (17)
- buffer encoding issue HOT 1
- Replace httpreq with xfetch HOT 5
- Module not found: HOT 1
- CORS issue HOT 2
- Interact with the Geolite2 database HOT 1
- How do you get around CORS not supported by Maxmind endpoint? HOT 1
- "userId" type HOT 3
- Passing parameters on call (Feature request) HOT 3
- An in-range update of dotest is breaking the build 🚨 HOT 2
- Keep Alive Option HOT 2
- Test ticket from Code Climate
- An in-range update of dotest is breaking the build 🚨 HOT 2
- Refactor test.js from a B on Code Climate
- Refactor geoip2ws.js from an A on Code Climate
- Refactor test.js from an A on Code Climate
- Client side usage HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nodejs-geoip2ws.