[์—๋Ÿฌ์ผ๊ธฐ] ๐Ÿ˜ˆ ๋น„๋™๊ธฐ ๋ฐ์ดํ„ฐ undefined

์ดํฌ์—ฐยท2025๋…„ 6์›” 4์ผ

๋‚ ์”จ์•ฑ ์ž‘์„ฑ์ค‘์—..
์ฝ˜์†”์ฐฝ์—์„  api๋กœ ๋‚ ์”จ data๋„ ์ž˜ ๋ถˆ๋Ÿฌ์™€์กŒ๊ณ 
๋„์‹œ ์ž…๋ ฅ์‹œ ์ž…๋ ฅ๋„ ์ž˜๋˜๊ณ  ํ•ด๋‹น๋˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ’๋„ ์ž˜ ๋ถˆ๋Ÿฌ์™€์ง€๋Š”๋ฐ
๋”ํ™”๋ฉด์—์„  ์•„๋ž˜์ฒ˜๋Ÿผ undefined๋กœ ๋ถˆ๋Ÿฌ์™€์ง€์ง€ ์•Š์Œ...........

์ž‘์„ฑํ•œ์ฝ”๋“œ.. ์•„๋ฌด๋ฆฌ ๋ด๋„ ๋ญ๊ฐ€ ์ž˜๋ชป๋œ์ง€ ๋ชฐ๋ž๋Š”๋ฐ... await..........!!!!!!!!

const getWeather = async (city) => {
  const res = await fetch(`${URL}/weather?q=${city}&appid=${KEY}&units=metric`);
  const weatherData = await res.json();
  if (!res.ok) {
    throw new Error("โŒ ๋‚ ์”จ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค.");
  }
  const { name } = weatherData;
  const { temp, temp_max: high, temp_min: low } = weatherData.main;
  const { main: weather } = weatherData.weather[0];
  console.log(name, temp, weather);
  return {
    name,
    temp,
    high,
    low,
    weather,
  };
};
const formEl = document.querySelector(".weatherForm");
const searchInput = document.querySelector(".city_search");
const weatherInfo = document.querySelector(".weather_info");
function onSubmit(e) {
  e.preventDefault();
  const city = searchInput.value;
  displayWeatherInfo(city);
}
function displayWeatherInfo(city) {
  try {
    const { name, temp, high, low, weather } = getWeather(city);
    console.log(name, temp, weather);
    weatherInfo.innerHTML = ` <div class="area">${name}</div>
          <div class="date">2025.8.5</div>
          <div class="temp">${temp}ยฐC</div>
          <div class="weather">${weather}</div>
          <div class="high_low">${low}ยฐC/${high}ยฐC</div>`;
  } catch (error) {
    weatherInfo.innerHTML = `<p>๋‚ ์”จ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค.</p>`;
  }
}
formEl.addEventListener("submit", onSubmit);
export default displayWeatherInfo; 

๋ฌธ์ œํ•ด๊ฒฐ๐Ÿ’ก๐Ÿ’ก๐Ÿ’ก๐Ÿ’ก๐Ÿ’ก๐Ÿ’ก๐Ÿ’ก๐Ÿ’ก๐Ÿ’ก


๐Ÿ‘‰ getWeather(ciry)์ด์ฝ”๋“œ๊ฐ€ ๋ฌธ์ œ...!!!!
getWeather๋Š” async ํ•จ์ˆ˜๋ผ์„œ, ํ˜ธ์ถœํ•˜๋ฉด Promise ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•จ.
๊ทธ๋ž˜์„œ displayWeatherInfo๋„ async ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค๊ณ  await์„ ์จ์„œ ๋ฐ์ดํ„ฐ๊ฐ€ ์˜ฌ ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ ค์•ผ ํ•œ๋‹ค...

await๋ฅผ ์•ˆํ•˜๊ฒŒ๋˜๋ฉด
name, temp ๋“ฑ์€ ์•„์ง ๋ฐ์ดํ„ฐ๊ฐ€ ๋„์ฐฉํ•˜์ง€ ์•Š์€ ์ƒํƒœ๋ผ์„œ undefined๊ฐ€ ๋˜๋Š”๊ฒƒ!!!

์ˆ˜์ •ํ–ˆ๋”๋‹ˆ ์ •์ƒ์ ์œผ๋กœ ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์™€์ง..!


๐Ÿ“Œ ๋น„๋™๊ธฐ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ํ๋ฆ„(๋น„๋™๊ธฐ API ๋ฐ์ดํ„ฐ ์š”์ฒญ ๋ฐ ์ฒ˜๋ฆฌ ์ˆœ์„œ)

  1. fetch๋กœ ๋‚ ์”จ ๋ฐ์ดํ„ฐ ์„œ๋ฒ„์—์„œ ์š”์ฒญ โ†’ ์ด๊ฑด ๋น„๋™๊ธฐ๋ผ ๊ธฐ๋‹ค๋ ค์•ผ ํ•จ
  2. response.json()์œผ๋กœ JSON ํŒŒ์‹ฑ โ†’ ์ด ๋˜ํ•œ ๋น„๋™๊ธฐ ์ž‘์—…
  3. ๋ฐ์ดํ„ฐ๊ฐ€ ์™„์ „ํžˆ ๋„์ฐฉํ•˜๊ณ  ๋‚˜์„œ์•ผ ๊ฐ€๊ณต ๊ฐ€๋Šฅ
  4. ๊ทธ ๊ฐ€๊ณต๋œ ๋ฐ์ดํ„ฐ๋ฅผ displayWeatherInfo() ๊ฐ™์€ ํ•จ์ˆ˜์— ๋„˜๊ฒจ์„œ ํ™”๋ฉด์— ๋ณด์—ฌ์ฃผ๊ธฐ

๊ทธ๋ž˜์„œ displayWeatherInfo ํ•จ์ˆ˜๊ฐ€ async์—ฌ์•ผ ํ•˜๊ณ , await getWeather(city)๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋‹ค๋ ค์•ผ ์ œ๋Œ€๋กœ ๊ฐ’์ด ๋‚˜์˜ด.

๋งŒ์•ฝ await ๋นผ๋ฉด Promise ๊ฐ์ฒด๊ฐ€ ๊ณง๋ฐ”๋กœ ๋ฐ˜ํ™˜๋ผ์„œ, ๋ฐ์ดํ„ฐ๊ฐ€ ์•ˆ ๋“ค์–ด์˜จ ์ƒํƒœ๋ผ์„œ undefined๊ฐ€ ๋œธ

profile
์ฝ”๋ฆฐ์ด๐ŸŒฑ / ๊ฐœ์ธ์ ์ธ ๊ณต๋ถ€๊ธฐ๋ก ๊ณต๊ฐ„์ž…๋‹ˆ๋‹ค, ์ œ๋กœ๋ฒ ์ด์Šค๋ถ€ํŠธ์บ ํ”„ ์ˆ˜๊ฐ•์ค‘...๐Ÿ˜„

0๊ฐœ์˜ ๋Œ“๊ธ€