无法重新分配变量实例javascript
我正在尝试从函数addResults获取结果,并将它们分别分配给this.lation和this.longitude。我一直没有定义。如何重新分配实例变量无法重新分配变量实例javascript,javascript,Javascript,我正在尝试从函数addResults获取结果,并将它们分别分配给this.lation和this.longitude。我一直没有定义。如何重新分配实例变量 // Fetch data from API class Weather { constructor(latitude, longitude) { this.latitude = latitude; this.longitude = longitude; } // fetch weat
// Fetch data from API
class Weather {
constructor(latitude, longitude) {
this.latitude = latitude;
this.longitude = longitude;
}
// fetch weather from API
async getWeather() {
const response = await fetch(`https://cors-anywhere.herokuapp.com/https://api.darksky.net/forecast/fe1ff29d5df09ef94625244329e7b5e4/${this.latitude},${this.longitude}`);
const responseData = await response.json();
return responseData;
}
addResults(results) {
this.latitude = results.results[0].geometry.lat;
this.longitude = results.results[0].geometry.lng;
}
}
下面是我在convertGeoLocation
函数中调用的app.js中的代码
// Init weather object
const weather = new Weather();
const ui = new UI();
const convertTypedLocation = new Convert('Redding', 'CA');
// Get weaterh on DOM load
document.addEventListener('DOMContentLoaded', getWeather);
// Get geolocation on load
document.addEventListener('DOMContentLoaded', convertGeoLocation);
function getWeather() {
weather.getWeather()
.then(results => {
// ui.paint(results);
console.log(results);
})
.catch(err => console.log(err))
}
// function getGeoLocation() {
// getLocation.getGeo()
// .then(geoResults => {
// ui.getLocation(geoResults);
// // console.log(geoResults);
// })
// .catch(err => console.log(err));
// }
function convertGeoLocation() {
convertTypedLocation.convertLocation()
.then(convertResults => {
weather.addResults(convertResults);
})
.catch(err => console.log(err));
}
在调用
getWeather()
如何以及在何处调用addResults?你传递给它的数据是什么?@NickParsons addResults在我的app.js文件中被调用。我从opencagedata传入API调用的结果,该API调用将城市和州位置转换为GPS坐标。显示调用它的代码。是不是在使用
wait
?@Barmar在调用addResults()
的下面添加了我的app.js文件。谢谢@Barmar只是一个跟进,然后我如何将获得的纬度和经度结果传递到我的天气对象中。现在我很难理解代码试图做什么。我假设convertGeoLocation
使用的是getWeather
的结果,但现在我看到它使用的是convertTypeLocation
。我试图完成的是从一个城市和州的用户那里获取输入。我正在使用convertTypedLocation
将输入转换为纬度和经度坐标。然后我想把纬度和经度作为参数传递给app.js文件顶部的weather
const。
// Init weather object
const weather = new Weather();
const ui = new UI();
const convertTypedLocation = new Convert('Redding', 'CA');
weather.addResults(convertTypedLocation.results[0].geometry.lat, convertTypedLocation.results[0].geometry.lng);
// Get weaterh on DOM load
document.addEventListener('DOMContentLoaded', getWeather);