我想在JavaScript中获取5天天气预报。
当我执行下面的代码时,我得到:
TypeError: Cannot read property 'temp' of undefined
如果将https://api.openweathermap.org/data/2.5/forecast?q=
更改为:
https://api.openweathermap.org/data/2.5/weather?q=
我得到当前天气没有错误。
为什么预测在我的情况下不起作用?我错过什么了吗?
JavaScript(我认为.then(data =>
代码段中存在问题):
var activities = document.getElementById('activitySelector');
var main = document.querySelector('#name');
var temp = document.querySelector('.temp');
var desc = document.querySelector('.desc');
activities.addEventListener('change', (e) => {
fetch('https://api.openweathermap.org/data/2.5/forecast?q='
+ e.target.value+'&appid=<<<<APIKEY>>>>&cnt=5')
.then(response => response.json())
.then(data => {
var tempValue = data['main']['temp'];
var nameValue = data['name'];
var descValue = data['weather'][0]['description'];
main.innerHTML = nameValue;
desc.innerHTML = "Desc - "+descValue;
temp.innerHTML = "Temp - "+tempValue;
})
.catch(err => alert(err));
})
HTML:
<select name="cities" id="activitySelector">
<div class="options">
<option class="option" value="warsaw">Warsaw</option>
<option class="option" value="phoenix">Phoenix</option>
<option class="option" value="berlin">Berlin</option>
</div>
</select>
<div class="container">
<div class="card">
<h1 class="name" id="name"></h1>
<p class="temp"></p>
<p class="clouds"></p>
<p class="desc"></p>
</div>
</div>
我正在尝试获取的JSON:
{"cod ":"200","消息":0,“cnt”:5、“清单”:[{" dt ":1618606800,“主”:{"temp":279.45,"feels_like":277.97,"temp_min":278.72,"temp_max":279.45,“压力”:1031,"sea_level":1031,"grnd_level":1028,“湿度”:56,"temp_kf":0.73},“天气”:[{"id ":802," main ":“云”、“描述”:"散云","图标":"03n "}]," clouds":{"全部":25},“风”:{" speed":2.06,“度”:80,“阵风”:6.1},“可见性”:10000,“啪”的一声:0," sys":{" pod":" n "}," dt_txt":“2021 - 04 - 16 21:00:00 "},{" dt":1618617600,“主”:{" temp":278.63," feels_like":277.46," temp_min":276.98," temp_max":278.63,“压力”:1031," sea_level":1031," grnd_level":1028,“湿度”:61," temp_kf":1.65},"天气":[{" id":801," main":“云”、“描述”:"几朵云","图标":" 02n "}]," clouds":{"全部":23},“风”:{" speed":1.65,“度”:53、“阵风”:4.49},“可见性”:10000,“啪”的一声:0," sys":{" pod":" n "}," dt_txt":“2021 - 04 - 17 00:00:00 "},{" dt":1618628400,“主”:{" temp":276.91," feels_like":275.48," temp_min":275.64," temp_max":276.91,“压力”:1031," sea_level":1031," grnd_level":1027,“湿度”:66," temp_kf":1.27},"天气":[{" id":800,“主”:“清除”、“说明”:"晴空","图标":" 01n "}]," clouds":{"全部":10},“风”:{" speed":1.65,“度”:21、“阵风”:4.06},“可见性”:10000,“啪”的一声:0," sys":{" pod":" n "}," dt_txt":“2021 - 04 - 17 03:00:00 "},{" dt":1618639200,“主”:{" temp":275.25," feels_like":273.27," temp_min":275.25," temp_max":275.25,“压力”:1031," sea_level":1031," grnd_level":1028,“湿度”:67," temp_kf":0},"天气":[{" id":800,“主”:“清除”、“说明”:"晴空","图标":" 01d "}]," clouds":{"全部":1},“风”:{" speed":1.89,“度”:16、“阵风”:4.72},“能见度”:10000,“啪”的一声:0," sys":{" pod":" d "}," dt_txt":“2021 - 04 - 17 06:00:00 "},{" dt":1618650000," main":{" temp":280.37," feels_like":278.43," temp_min":280.37," temp_max":280.37,“压力”:1030," sea_level":1030," grnd_level":1027,“湿度”:44," temp_kf":0},"天气":[{" id":800,“主”:“清除”、“说明”:"晴空","图标":" 01d "}]," clouds":{"全部":2},“风”:{" speed":2.84,“度”:39、“阵风”:4.51},“可见性”:10000,“啪”的一声:0," sys":{" pod":" d "}," dt_txt":“2021 - 04 - 17 09:00:00 "}],"城市":{" id":2643743,“姓名”:"伦敦"," coord":{" lat":51.5085,“lon”:-0.1257},“国家”:“GB”,“population”:1000000,"时区":3600,“日出”:1618549331,“日落”:1618599475
2条答案
按热度按时间x6492ojm1#
问题仅仅是你取消引用你想要的值的方式。
data
具有属性list
。list
是一个对象数组,每个对象都有一个main
键,该键指向一个具有temp
键的对象。例如,您可以抓取 * 所有 * 元素并记录它们的
main.temp
字段,如下所示:zzwlnbp82#
所以你有
在我看到的文档中
所以我假设你在你的URL中缺少了/daily。