Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

355
Views
Why can't my JavaScript Class's method call a neighbouring method?

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 ๐Ÿ™

about 4 years ago ยท Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!