javascript - 创建类以获取天气
问题描述
请帮忙,我创建了主类来获取四天的天气,但控制台抛出错误 - 未定义 getCoordinates。问题是什么?还是函数init错了?我有函数 getCoordinates,而不是获取城市并返回纬度和经度,然后应该将它传递给函数 get WeatherForeCast 以获取天气,然后从该函数将数据传递给函数 renderForeCastInfo 以获取四天的天气。怎么了?
class Weather {
async getCoordinates() {
//code
}
async getWeatherForecast(locationCoordinates) {
//code
}
fahrenheitToCelsius(temp) {
//code
}
renderForecastInfo(currently, daily) {
//code
}
init() {
getCoordinates().then(coords => {
return getWeatherForecast(coords);
}).then((forecast) => {
const { currently, daily } = forecast;
renderForecastInfo(currently, daily);
});
}
}
解决方案
您必须参考this
成员函数。请看下面的代码。
class Weather {
constructor(city) {
this.city = city
}
async getCoordinates(city) {
//code
}
async getWeatherForecast(locationCoordinates) {
//code
}
fahrenheitToCelsius(temp) {
//code
}
renderForecastInfo(currently, daily) {
//code
}
init() {
this.getCoordinates().then(coords => {
return this.getWeatherForecast(coords);
}).then((forecast) => {
const { currently, daily } = forecast;
this.renderForecastInfo(currently, daily);
});
}
}
const w1= new Weather();
w1.init();
推荐阅读
- javascript - 从nodeJs服务器获得响应后如何调用函数(使用javascript)?
- python - AttributeError:“NoneType”对象没有名为“find_all”的属性
- r - 根据 R 中的某些条件将缺失的日期添加到数据表中
- python - Python:动态分箱熊猫中的列
- java - 如何在启动 android 应用程序时启动特定片段
- python - 正则表达式 - 仅选择字符串中的单独单词
- python - 如何将具有混合 unicode 表示和 unicode 的文本文件转换为字节?
- python - 从多个 python 模块访问公共属性
- node.js - 使用 jest 在猫鼬中预钩子的单元测试用例
- java - 我们如何在 Android/java 的 Firestore Map 类型数组中推送新元素?