我想从功能中获取数据并将其传递到提取网址,以获取城市数据
这是我下面正在处理的代码。我试图从第一个URL获取纬度和经度数据,以便我可以将其传递到一个新的提取请求中,该请求需要LAT和LON来抓住5天预测的城市中的天气数据。但是我不确定如何从一个函数中调用数据并将其作为字符串传递为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)
}
}
This is the code that I am working on below. I am trying to grab the latitude and longitude data from the 1st url, so that I can pass that into a new fetch request that takes lat and lon to grab weather data on a city with a 5 day forecast. But I am unsure how to call the data from one function and pass it as a string in the 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)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
粗略:
您可以打开
{}
使用多个代码的范围。Roughly:
You can open a
{}
scope to use multiple lines of code.