首页 > 解决方案 > 这个对 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)
                })
        
       })
        
        
        }
    });
    

标签: javascriptnode.jsweb

解决方案


谢谢大家的帮助,但我想通了。答案是进行第二次提取并将其放入“.then”


推荐阅读