Javascript 什么';WeatherApi的请求有什么问题
这是一个天气应用程序,我了解它,我有一个问题,所以你能帮我吗 它在获取id时给出错误 我在错误所在的地方留下了一条评论,请检查一下 谢谢Javascript 什么';WeatherApi的请求有什么问题,javascript,node.js,web,Javascript,Node.js,Web,这是一个天气应用程序,我了解它,我有一个问题,所以你能帮我吗 它在获取id时给出错误 我在错误所在的地方留下了一条评论,请检查一下 谢谢 const form = document.querySelector("form"); const input = document.querySelector("input"); const div = document.querySelector("div") window.addE
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)
})
})
}
});
看起来在承诺中分配了id,但承诺未解决。您可以将window.addEventListener()的回调函数设为异步,然后在所有fetch()调用之前加上wait关键字。嘿,我唯一能弄清楚的是,您可能正在向weather api传递一个未定义的id。请查看文档或共享注释中的链接,让我看看。
谢谢快乐编码谢谢大家的帮助,但我想起来了。 答案是把第二次取回的东西放进去