Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   Ошибка в коде (https://javascript.ru/forum/misc/78904-oshibka-v-kode.html)

maplol123@gmail.com 20.11.2019 23:30

Ошибка в коде
 
Мне надо реализовать погоду в городах, но выдает ошибку
- Cannot read property 'name' of undefined
Можно ли как-то по-другому обратиться к Name и к остальной части?
let url = "https://api.openweathermap.org/data/2.5/weather?q=",
    gomelurl= url+"homyel&APPID=a8de50e25c5a720cf60c585bda8206d6",
    gomel = document.getElementById("gomel"),
    name,weather,temp;

function getelem(x,y) {
    let request = new XMLHttpRequest();
    request.addEventListener("load", x);
    request.open("GET", y, true);
    request.responseType = "json";
    request.send("");
    return(x,y);
}
function change(city) {
    name = this.response.name;
    weather = this.response.weather[0].main;
    temp = (Math.round(this.response.main.temp - 273.15));
    city.value =  "City: "+name +"\nWeather: "+weather+"\nTemp: "+ temp+ "°C"
}

getelem(change(gomel),gomelurl);

destus 21.11.2019 06:56

function change(city) {
    return function() {
        name = this.response.name;
        weather = this.response.weather[0].main;
        temp = (Math.round(this.response.main.temp - 273.15));
        city.value = "City: " + name + "\nWeather: " + weather + "\nTemp: " + temp + "°C"
    }
}

maplol123@gmail.com 21.11.2019 16:58

destus,
Спасибо большое!


Часовой пояс GMT +3, время: 10:11.