Я импортирую функцию getWeather
из другого файла .js, который я написал. Результатом является BLOB-объект JSON. Я проверил, что я возвращаю BLOB-объект JSON, но когда я пытаюсь вызвать функцию getWeather
и использовать .then для ожидания ответа и установки своего состояния, я получаю TypeError.
getWeather(parseFloat(lat),parseFloat(long)).then((data) =>{
this.setState({weatherType: data.currently.icon, temp: data.currently.temperature})
return data
}).catch((error) => {
console.log(error.message);
})
Функция getWeather
здесь:
export function getWeather(lat,long) {
const params = {
headers: {
"content-type": "application/json; charset=UTF-8",
"Access-Control-Allow-Origin": "*"
},
method: "GET"
};
fetch(`https://api.com/mykey/${lat},${long}`, params)
.then(function (data) {
return data.json();
})
.then(res => {
console.log(res);
})
.catch(error => {
console.log(error);
});
}