Este es el código en el que estoy trabajando a continuación. Estoy tratando de obtener los datos de latitud y longitud de la primera URL, para poder pasarlos a una nueva solicitud de recuperación que toma la latitud y la longitud para obtener datos meteorológicos en una ciudad con un pronóstico de 5 días. Pero no estoy seguro de cómo llamar a los datos de una función y pasarlos como una cadena en la URL.
let weather = { "apiKey": "", "city": " ", fetchWeather: function(city) { // this fetch will call the api to grab the Lon and lat, using the function displayLatlon fetch("https://api.openweathermap.org/data/2.5/weather?q=" + city + "&units=imperial&appid=" + this.apiKey) .then(response => response.json()) .then(data => this.displaylatlon(data)) // This fetch will use the API to grab the weather conditions for the city based on it's lat and lon fetch("https://api.openweathermap.org/data/3.0/onecall?lat=" + **lat** + "&lon=" + **lon** + "&exclude=hourly,daily&appid=" + this.apiKey) .then(response => response.json()) .then(weatherData =>console.log(weatherData)) }, // Function to grab the latitude and longitude of the city **displaylatlon: function(data) { const {lat, lon} = data.coord const{temp} = data.main** console.log(lat, lon) } }Apenas:
fetch("https://api.openweathermap.org/data/2.5/weather?q=" + city + "&units=imperial&appid=" + this.apiKey) .then(response => response.json()) .then(data => { const {lat, lon} = data.coord; return fetch("https://api.openweathermap.org/data/3.0/onecall?lat=" + **lat** + "&lon=" + **lon** + "&exclude=hourly,daily&appid=" + this.apiKey) }) .then(response => response.json()) .then(weatherData =>console.log(weatherData)) Puede abrir un alcance {} para usar varias líneas de código.