It seems the buildGeoLocation()
method cannot run this.requestHandler()
but I can't seem to work out why.
The console shows Uncaught TypeError: this.requestHandler is not a function
.
Below is my code:
class Geolocation {
constructor() {
this.elements = {
section: document.querySelector('.james-section'),
button: document.querySelector('.james-button'),
}
this.elements.button.addEventListener('click', this.buildGeoLocation)
}
buildGeoLocation() {
Promise.all([
this.requestHandler("https://freegeoip.app/json/"),
this.requestHandler("geolocation-template.html")
]).then(function(values) {
const data = JSON.parse(values[0]);
const markup = values[1]
.replace('[ip]', data.ip)
.replace('[city]', data.city)
.replace('[country]', data.country_name)
.replace('[lat]', data.latitude)
.replace('[lng]', data.longitude);
elements.section.innerHTML = markup;
}).catch(function(reason) {
console.log(reason);
});
}
requestHandler(url) {
return new Promise(function(resolve, reject) {
const xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if(this.readyState == 4) {
if(this.status == 200) {
resolve(this.responseText);
} else {
reject('Call Failed');
}
}
};
xhttp.open("GET", url);
xhttp.send();
});
}
}
new Geolocation;
I'm quite new to using Classes so any pointers would be much appreciated