I need some help with an async await fetch call

I am using openweathermap.org to get the weather for a hard-coded city name. I created a function before that fetch call using zip code and I am getting and returning the city name from that function. However, I can't seem to use the return value to set the city name. I obviously do not have promises down because I'm getting Promise pending when I console log the function call. I have the code in question in CodePen: https://codepen.io/jim-kernicky/pen/ZERmxaM What am I doing wrong?
10 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Kernix
Kernix2y ago
Thanks for t he reply. But how do I get the city name and use it in a variable which I them plug into the API endpoint? That's where I'm getting stuck. The zip API endpoint gives me the city name and lat/long if I wanted to use that. I already have the city name endpoint working, so I just want to concat the variable name which is equal to the city name that's it. Then a user can enter a zip code and the resulting city can be used to get the weather data:
city = 'Philadelphia';
const currentWeatherCity = 'https://api.openweathermap.org/data/2.5/weather?q=' + city + '&units=imperial&APPID={API_ID}'
fetchCurrent(currentWeatherCity);
city = 'Philadelphia';
const currentWeatherCity = 'https://api.openweathermap.org/data/2.5/weather?q=' + city + '&units=imperial&APPID={API_ID}'
fetchCurrent(currentWeatherCity);
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Kernix
Kernix2y ago
No, I didn't want to post my actual key
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Kernix
Kernix2y ago
Basically, I am using one openweathermap endpoint to get a field which I need in my other endpoints. I'm not an expert at this stuff so I'm sure I'm just handling it wrong
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Kernix
Kernix2y ago
Same API source (openweathermap.org). They have a lot of different endpoints. I successfully used the one where you add the city name into the endpoint string. But that is hard-coded. I would like a user to: 1. enter a zip code 2. get a city name (or lat/long coords) 3. use that data to add the city name into the weather api endpoint for city name 4. display weather - DONE
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Kernix
Kernix2y ago
Oh, okay let me try that... Whew, I made a lot of changes but it finally worked - THANKS! I'm hard-coding the zip for now but I'll eventually add a form element to grab it to get the weather, although I assume I'll need another async/await function to get that. Although I do have navigator.geolocation to grab the users location and I can have that default to the initial weather maybe. I send a support question to openweathermap about city names that are 2 works - I do not get ant data back. It appears that %20 is inserted between the words. I searched their docs but did not see anything yet about how to handle that. I may have to get the users zip, get their lat/long and proceed that way.
Want results from more Discord servers?
Add your server