Hi there, I'm encountering an issue with the code snippet below. It keeps throwing an error message that says "Property 'weatherData' does not exist on type 'XMLHttpRequest'." The purpose of this code is to display weather information in a console log.
class WeatherService {
public weatherData;
public getWeather(callback) {
let url = `http://api.openweathermap.org/data/2.5/weather?API=&APPID=d43debb0b9a3919fef3f0f689e82583e&q=${this.city}`;
let request = new XMLHttpRequest();
request.addEventListener('load', function() {
// parsing weather data from the Ajax call
this.weatherData = JSON.parse(request.responseText);
// calling back to indicate completion
callback();
})
request.open('GET', url);
request.send();
}
constructor(private city: string) { }
}
// creating a new instance of the WeatherService class for Seattle
let service = new WeatherService('Seattle');
// running the service method to fetch weather data for Seattle
service.getWeather(() => {
console.log(service.weatherData);
});