Reputation: 11
const request = require('request');
var geocodeAddress = (address) => {
var encodedAddress = encodeURIComponent(address);
request({
url: `http://www.mapquestapi.com/geocoding/v1/address?key=APIKey&location=${encodedAddress}`,
json: true
}, (error, response, body) => {
if (error) {
console.log('Unable to connect to Google servers');
} else if (body.results[0].locations[0].postalCode.length < 5) {
console.log('Not a valid length of zip code.')
} else if (body.results[0].locations[0].postalCode < 501) {
console.log('That zip code does not exist')
} else {
console.log('Lat: ' + body.results[0].locations[0].latLng[0])
console.log('Lng: ' + body.results[0].locations[0].latLng[1])
}
});
};
module.exports.geocodeAddress = geocodeAddress;
the other file is the one I use to execute the application.
const yargs = require('yargs');
const geocode = require('./geocode/geocode');
const argv = yargs
.options({
a:{
demand: true,
alias: 'Address',
describe: 'Address to fetch weather for',
string: true
}
})
.help()
.argv;
geocode.geocodeAddress(argv.address)
when i run the second one in terminal, it says the zip code isn't long enough. I take that out, it spits out the next error, i take that out and the next one. No matter what I do it won't work. this code was working minutes before i put it in a function. I'm only a beginner and new to stackoverflow and I've been continuously trying to fix this problem, but I can't find a fix, so here I am. LMK if there is a problem with my formatting, I'm new to the forum, and most importantly, if you see any problems in the code, LMK!! Thanks in advance
Upvotes: 1
Views: 51
Reputation: 1213
Looks like your code is fine, it's what you're expecting the data to be that is incorrect.
A simple console.log
will show you that most of the returned locations contain no zipcode.
For instance, replace all your else if
s with an else
block that only contains console.dir(body.results[0].locations[0])
. I tried out a couple; here's what the string Houston
returns:
{
street: '',
adminArea6: '',
adminArea6Type: 'Neighborhood',
adminArea5: 'Houston',
adminArea5Type: 'City',
adminArea4: 'Harris County',
adminArea4Type: 'County',
adminArea3: 'TX',
adminArea3Type: 'State',
adminArea1: 'US',
adminArea1Type: 'Country',
postalCode: '',
geocodeQualityCode: 'A5XAX',
geocodeQuality: 'CITY',
dragPoint: false,
sideOfStreet: 'N',
linkId: '282040105',
unknownInput: '',
type: 's',
latLng: { lat: 29.760803, lng: -95.369506 },
displayLatLng: { lat: 29.760803, lng: -95.369506 },
mapUrl: <redacted>
}
Note that the postalCode
is the empty string which will fail all of your code's expectations.
Also, it's usually a bad idea to share your API key! Make sure you either change yours or setup permissions restricting what domains the requests can come from.
Now if you're only interested in locations with postal codes, filter all the others out:
let locations = body.results[0].locations;
let locationsOfInterest = locations.filter(location => (location.postalCode !== ''));
It's also worthwhile to note that your code probably does work for specific strings. For instance passing in Boston Public Library
returns an address with a valid zipcode.
Upvotes: 2