๐ŸŽฌ ์˜ํ™” ๊ฒ€์ƒ‰ ์‚ฌ์ดํŠธ๋ฅผ ๋งŒ๋“ค๋ฉฐ ๋ฐฐ์šด ๊ฒƒ๋“ค

์›์ง€ยท2022๋…„ 11์›” 4์ผ
0

์Šคํ„ฐ๋”” ํšŒ๊ณ 

๋ชฉ๋ก ๋ณด๊ธฐ
1/5
post-thumbnail

OMDB API (The Open Movie Database)๋ฅผ ํ™œ์šฉํ•ด ์˜ํ™” ๊ฒ€์ƒ‰ ์‚ฌ์ดํŠธ ๋งŒ๋“ค๋ฉฐ ๋ฐœ์ƒํ•œ ๋ฌธ์ œ์™€ ๊ทธ ํ•ด๊ฒฐ ๊ณผ์ •์„ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค !

1. X is not iterable

API๊ฐ’์œผ๋กœ ์˜ํ™” ์ •๋ณด๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ์ค‘ ๋ฐœ์ƒํ•œ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค..

์˜ํ™” ์ œ๋ชฉ์„ ๊ฒ€์ƒ‰ํ•  ๊ฒฝ์šฐ, ๊ฒ€์ƒ‰ํ•œ ์ œ๋ชฉ์ด ํฌํ•จ๋œ ๋ชจ๋“  ์˜ํ™”๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๊ฐ€ ๋ฐ˜ํ™˜๋˜์ง€ ์•Š์•˜๊ณ , ์ฝ˜์†”์ฐฝ์—๋Š” โ€˜movies(์˜ํ™” ์ •๋ณด๋ฅผ ๋‹ด์€ ๊ฐ์ฒด) is not iterableโ€™์ด ์ฐํ˜”์Šต๋‹ˆ๋‹ค ๐Ÿ’ข


๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ณ ์ž ๊ตฌ๊ธ€๋ง์„ ํ•ด๋ณด๋‹ˆ

you have to use Object.keys orย Object.entries, to iterate over the properties or entries of an object.

๋ผ๋Š” ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

์ œ๊ฐ€ ์ดํ•ดํ•œ ๋ฐ”๋กœ๋Š” .. 'movies๊ฐ€ ์ˆœํšŒ ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ๊ฐ์ฒด์˜ key๊ฐ’ ํ˜น์€ value๊ฐ’์„ ํ†ตํ•ด ์ˆœํšŒ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋„๋ก ๋งŒ๋“ค์–ด๋ผ!' ๋ผ๋Š” ๋œป์ธ ๋“ฏ ํ•˜์—ฌ .. const movie of Object.values(movies)์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ์š”

์ด๋ฒˆ์—” TypeError: Cannot convert undefined or null to object๋ผ๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.

์•„๋ž˜์˜ getMovies ํ•จ์ˆ˜๋Š” ๋ฐ”๋กœ ๋ฌธ์ œ์˜ ๊ทธ ํ•จ์ˆ˜๋กœ API์—์„œ ์˜ํ™” ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์™€์•ผ ํ•˜๋Š” ์•„์ด์ž…๋‹ˆ๋‹ค.

async function getMovies(page = "", title = "", year = "") {
 const url = `https://omdbapi.com/?apikey=7035c60c&s=${title}&page=${page}&y=${year}`;
 console.log(url);
 const res = await fetch(url);
 const { Search: movies } = await res.json();
 console.log(movies);
 return movies;
}

์ €์˜ ๊ฒฝ์šฐ page, title, year ๊ฐ’์„ parameter๋กœ ๋„ฃ์—ˆ๊ณ , ์–ด๋–ค ๊ฐ’์ด ๋น„์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด movies๋ฅผ ์ฝ˜์†”์— ์ฐ์–ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ, page๋Š” ์ •์ƒ์ ์œผ๋กœ ์ถœ๋ ฅ๋˜์ง€๋งŒ title๊ณผ year์ด ๋น„์–ด์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค.

OMDB API ์‚ฌ์ดํŠธ๋ฅผ ํ™•์ธํ•ด๋ณด๋‹ˆ parameter ์ค‘ title์€ ํ•„์ˆ˜๊ฐ’์œผ๋กœ ๋ฐ˜๋“œ์‹œ ํ•„์š”ํ•œ ์ธ์ž๋ผ๋Š”๋ฐ์š”.. ์ €๋Š” title์ด ๋นˆ๊ฐ’์ด๋ผ ๋ฐฐ์—ด์ด ๋ฐ˜๋ณต๋˜์ง€ ์•Š๊ณ  undefined๋ฅผ ๊ณ„์† ๋ฑ‰์–ด๋‚ธ ๊ฒƒ์ด์—ˆ์Šต๋‹ˆ๋‹ค ์•„๋†”

async function getMovies(page = "", title = "frozen", year = "") {
  const url = `https://omdbapi.com/?apikey=7035c60c&s=${title}&page=${page}&y=${year}&type=${genre}`;
  const res = await fetch(url);
  const { Search: movies } = await res.json();
  console.log(movies);
  return movies;
}

์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์˜ํ™” ๊ฒ€์ƒ‰ ์‹œ ์ œ๋ชฉ์„ ์ž…๋ ฅํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ alert์ฐฝ์„ ๋„์›Œ ์ œ๋ชฉ์„ ํ•„์ˆ˜๋กœ ์ž…๋ ฅํ•˜๋„๋ก ํ•˜๊ณ , title์ด ๋น„์–ด ๋ชจ๋“  ๊ธฐ๋Šฅ์ด ์ค‘๋‹จ๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๊ณ ์ž ์ œ๋ชฉ์˜ ๊ธฐ๋ณธ๊ฐ’์„ frozen์œผ๋กœ ์ง€์ •ํ•˜์ž ์˜ํ™”๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์ถœ๋ ฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค !

2. select์˜ ๊ธฐ๋ณธ๊ฐ’

์ œ๋ชฉ ๊ฒ€์ƒ‰์„ ํ†ตํ•ด ์˜ํ™” ์ •๋ณด ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ๋ฏธ์…˜์„ ์ˆ˜ํ–‰ํ•œ ๋’ค, ๊ฐœ๋ด‰์—ฐ๋„๋กœ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•˜๊ณ ์ž ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ selectํƒœ๊ทธ๋ฅผ ์ถ”๊ฐ€ํ•˜์ž ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋กœ 2022๋…„ ์˜ํ™”๋งŒ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋ฌธ์ œ์ ์ด ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ๊ฐ€ ๋ฌด์—‡์ธ๊ณ  ์ฐพ์•„๋ณด๋‹ˆ ..

์ €๋Š” ์„ค์ •ํ•œ ์  ์—†๋Š” option์˜ ๊ธฐ๋ณธ๊ฐ’์ด 2022๋…„์œผ๋กœ ์ง€์ •๋˜์–ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค

//์ฒซ๋ฒˆ์งธ ๋ฐฉ๋ฒ•
<option value="">All year</option>
//๋‘๋ฒˆ์งธ ๋ฐฉ๋ฒ•
<option value="all-year" selected>All year</option>

select๋Š” ๊ธฐ๋ณธ๊ฐ’์„ ์ง€์ •ํ•˜๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ ํ•ด๋‹น option์˜ value๊ฐ’์„ ๋น„์›Œ๋†“๊ฑฐ๋‚˜, ๊ธฐ๋ณธ์œผ๋กœ ์ง€์ •ํ•˜๊ณ  ์‹ถ์€ ๊ฐ’์˜ option ํƒœ๊ทธ์— selected๋ฅผ ์ ์œผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒฝ์šฐ ๊ฐ€์žฅ ์ƒ๋‹จ์˜ option์ด ๊ธฐ๋ณธ๊ฐ’์ด ๋ฉ๋‹ˆ๋‹ค.

์ €๋Š” ์˜ํ™” ์ œ๋ชฉ์„ ๊ฒ€์ƒ‰ํ•  ๋•Œ ๋ชจ๋“  ์—ฐ๋„์˜ ์˜ํ™”๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฒƒ์ด ๊ธฐ๋ณธ๊ฐ’์ด ๋˜์–ด์•ผ ํ•˜๋Š”๋ฐ, ๊ธฐ๋ณธ๊ฐ’์„ ๋”ฐ๋กœ ์ง€์ •ํ•˜์ง€ ์•Š์•„ ๊ฐ€์žฅ ์ƒ๋‹จ์˜ 2022๋…„์ด ๊ธฐ๋ณธ๊ฐ’์ด ๋˜์–ด 2022๋…„ ์˜ํ™”๋งŒ์„ ๋ถˆ๋Ÿฌ์™€ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒƒ์ด์—ˆ์Šต๋‹ˆ๋‹ค ..^^

3. X.classList.add('hidden')์ด ์ ์šฉ๋˜์ง€ ์•Š์Œ

.hidden{
	display: none;
}
moreBtn.classList.add('hidden')

๋”๋ณด๊ธฐ ๋ฒ„ํŠผ์€ ๊ฒฝ์šฐ์— ๋”ฐ๋ผ ๋ณด์ด๊ฑฐ๋‚˜ ๋ณด์ด์ง€ ์•Š๋„๋ก ๊ตฌํ˜„ํ•ด์•ผํ–ˆ์Šต๋‹ˆ๋‹ค. ์ฒ˜์Œ์—๋Š” ์ด๊ฒƒ์„ hidden class๋กœ ๊ด€๋ฆฌํ•˜๋ ค ํ–ˆ์ง€๋งŒ, ๋”๋ณด๊ธฐ ๋ฒ„ํŠผ์— ์ด๋ฏธ display: flex๊ฐ€ ์„ค์ •๋˜์–ด ์žˆ์–ด hidden class๊ฐ€ ๋จนํžˆ์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.
/* ์ด์ „ ์ฝ”๋“œ */
main .actions .more-btn {
  display: flex
}
/* ๋ฐ”๋€ ์ฝ”๋“œ */
.more-btn {
  display: flex
}

css ์ ์šฉ์‹œ ๊ฐ™์€ ์šฐ์„  ์ˆœ์œ„์— ์žˆ๋Š” ๊ฒฝ์šฐ, ๋ถ€๋ชจ-์ž์‹ ๊ด€๊ณ„๊ฐ€ ๋งŽ์€ ๊ฒฝ์šฐ๊ฐ€ ๋จผ์ €์ด๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ž˜์™€ ๊ฐ™์ด ๋ถ€๋ชจ ์š”์†Œ๋ฅผ ์ง€์šฐ๊ณ  ํ•ด๋‹น class๋งŒ ๋‚จ๊ฒจ๋‘์ž hidden class๊ฐ€ ๋ฐ”๋กœ ์ ์šฉ๋˜์—ˆ์Šต๋‹ˆ๋‹ค !



์•„์ง ๋ชจ๋ฅด๋Š” ๊ฒƒ ํˆฌ์„ฑ์ด๋ผ ๊ฐˆ ๊ธธ์ด ๋ฉ€์ง€๋งŒ .. ^^ ํ•˜๋‚˜ ๋‘˜์”ฉ ๋ฌธ์ œ์ ์„ ์ฐพ์•„๊ฐ€๋ฉฐ ๊นจ๋‹ฌ์Œ์„ ์–ป๊ณ ์žˆ๋Š” ์ค‘์ž…๋‹ˆ๋‹ค .. ์‚ฌ์‹ค js ์ด๋ก  ๊ฐ•์˜๋งŒ ๋“ฃ๋‹ค๊ฐ€ ์ด๋ ‡๊ฒŒ ์ง์ ‘ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋ ค๋‹ˆ ์–ด๋ ค์›€์ด ๋งŽ์•˜๋Š”๋ฐ์š” ใ…œ ๋‚˜๋ฆ„ ์žฌ๋ฏธ๋„ ์žˆ์–ด์„œ ๋นจ๋ฆฌ ๋”๋” ๋งŽ์€ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•ด ๋ณด๊ณ  ์‹ถ๋‹ค๋Š” ์š•์‹ฌ์ด ๋“ค์–ด์š” ! ์—ด์‹ฌํžˆ ๊ณต๋ถ€ํ• ๊ฒŒ์š” ๋น ์ƒค

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