Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 什么';WeatherApi的请求有什么问题_Javascript_Node.js_Web - Fatal编程技术网

Javascript 什么';WeatherApi的请求有什么问题

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

这是一个天气应用程序,我了解它,我有一个问题,所以你能帮我吗

它在获取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)
                })
        
       })
        
        
        }
    });
    

看起来在承诺中分配了id,但承诺未解决。您可以将window.addEventListener()的回调函数设为异步,然后在所有fetch()调用之前加上wait关键字。

嘿,我唯一能弄清楚的是,您可能正在向weather api传递一个未定义的id。请查看文档或共享注释中的链接,让我看看。
谢谢快乐编码

谢谢大家的帮助,但我想起来了。 答案是把第二次取回的东西放进去