javascript - 错误 - 函数不是子类中的函数
问题描述
我有一个父类 Weather,它显示了 4 天的天气。我有一个子类,其中包含一系列城市。他应该在这个数组中显示一个随机城市的 4 天的天气。但我有一个错误 - this.makeRandom(...).then 不是函数。怎么了?
class GetRandomCity extends Weather {
constructor(city) {
super();
this.city = city;
}
makeRandom() {
//code
};
init() {
this.makeRandom().then(city => {
this.getCoordinates(city)
}).then(coords => {
return this.getWeatherForecast(coords);
}).then((forecast) => {
const { currently, daily } = forecast;
this.renderForecastInfo(currently, daily);
});
}
}
const w1 = new GetRandomCity(['Paris', 'Minsk', 'Madrid', 'Chikago']);
解决方案
原因是因为makeRandom
不返回承诺。getCoordinates
如果您选择使其成为async
函数,则需要在之前放置一个 return 语句。否则,您可以将init
函数中的代码更改为此,它应该可以工作:
const city = this.makeRandom();
this.getCoordinates(city).then(coords => {
return this.getWeatherForecast(coords);
}).then(forecast => {
const { currently, daily } = forecast;
this.renderForecastInfo(currently, daily);
});
推荐阅读
- angular - 将 RxJS 可观察对象与第一个运算符结合起来
- python - Raspberry Pi spidev 自动在使用中
- assembly - 如何将 999 添加到 nASM x32 中的任何 3 位数字?
- javascript - .filter 不适用于带有 javascsript 中对象的数组
- google-sheets - 提交表单时填充公式
- flutter - Fluter,隐藏虚拟键盘时文本字段值变空
- sql - 内连接 2 个值(日期时间和 id)同一张表
- oracle - 如何以 HTML 格式保存包含数据的报告?(OBIEE 12c)
- javascript - 在反应应用程序中运行 npm run dev 命令时出错
- java - 使用 Java 8 构建的 GraalVM 本机映像失败,并出现错误 com.oracle.svm.core.hub.DynamicHub.getGenericInfo() 方法在此平台不可用