javascript - 这个对 WeatherApi 的请求有什么问题
问题描述
这是关于我了解它的天气应用程序,但我遇到了问题,所以你能帮我解决吗
它在使用 id 时出错 我在出错的地方留下了评论 请检查并谢谢
const form = document.querySelector("form");
const input = document.querySelector("input");
const div = document.querySelector("div")
window.addEventListener("load", ()=>{
let lat;
let long;
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(position =>{
lat = position.coords.longitude;
long = position.coords.latitude;
let id;
const apiSearchByLat_Long = `https://cors-anywhere.herokuapp.com/https://www.metaweather.com/api/location/search/?lattlong=${long},${lat}`
console.log(long)
console.log(lat)
fetch(apiSearchByLat_Long)
.then(response =>{
return response.json();
})
.then(data =>{
id = data[0].woeid
console.log(id);
})
const apiSearchById = `https://cors-anywhere.herokuapp.com/https://www.metaweather.com/api/location/${id}/`
fetch(apiSearchById)
//the error is here it tells me "https://cors-anywhere.herokuapp.com/https://www.metaweather.com/api/location/undefined/"
.then(response =>{
return response.json();
})
.then(data =>{
console.log(data)
})
})
}
});
解决方案
谢谢大家的帮助,但我想通了。答案是进行第二次提取并将其放入“.then”
推荐阅读
- c# - Selenium Firefox 首选项已更改但未应用
- jquery - 根据响应数据的某些条件隐藏jQuery Datatable中的特定列
- python-3.x - 文件大小太大
- python - Pickle (Python 3.6) 写入空文件
- python - python 3.6 从列表中获取文本
- sql - 在日志条目表中建立关系的最高效方式?
- python - Python 使用 COM 自动化 Fusion 360
- javascript - 使用 Vivus.js 和 GSAP 对最初隐藏的元素进行动画处理
- php - str_replace 特殊字符场景
- powershell - 从 AD 检索 Bitlocker 恢复密钥