๋ชฉ๋กWeb (8)

Hello Ocean! ๐ŸŒผ

[VanillaJS] map๊ณผ forEach

JS์—์„œ array์˜ ๊ฐ ์›์†Œ์— ๋Œ€ํ•ด ๊ฐ™์€ ์ž‘์—…์„ ๋ฐ˜๋ณตํ•˜๊ณ  ์‹ถ์„ ๋•Œ, for๋ฌธ ๋Œ€์‹ ์— Array.prototype.map()์ด๋‚˜ Array.prototype.forEach()๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‘ ํ•จ์ˆ˜์˜ ์ฐจ์ด๋Š” ๋ฌด์—‡์ผ๊นŒ? MDN ๊ณต์‹๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•ด๋ณด์ž. map() : ๋ฐฐ์—ด ๋‚ด์˜ ๋ชจ๋“  ์›์†Œ ๊ฐ๊ฐ์— ๋Œ€ํ•˜์—ฌ ์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ์•„ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ * ๊ธฐ์กด ๋ฐฐ์—ด์˜ ์›์†Œ๋“ค๋กœ ์ž‘์—…์„ ์™„๋ฃŒํ•œ ๋’ค ํ•ด๋‹น ๊ฒฐ๊ณผ๋ฌผ๋“ค์„ ๋ชจ์•„ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ returnํ•ด์ค€๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. * ๋”ฐ๋ผ์„œ map์˜ ์šฉ๋„๋ฅผ ์ œ๋Œ€๋กœ ์‚ด๋ฆฌ๋ ค๋ฉด callback ํ•จ์ˆ˜ ๋‚ด์— return ๊ตฌ๋ฌธ์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. forEach() : ์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ ๋ฐฐ์—ด ์š”์†Œ ๊ฐ๊ฐ์— ๋Œ€ํ•ด ์‹คํ–‰ * forEach()๋Š” ๋ฐฐ์—ด์„ ๋ณ€ํ˜•ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ callbac..

Web 2021. 10. 1. 23:58
[Postman] request์† cookie undefined error ํ•ด๊ฒฐ

login api ์—์„œ cookie๋ฅผ ์„ค์ •ํ•ด์ฃผ๊ณ , ๋‹ค๋ฅธ api๋ฅผ ๋ณด๋‚ผ ๋•Œ ํ•ด๋‹น cookie ๊ฐ’์„ ์ฝ์–ด์„œ ํ™œ์šฉํ•˜๋ ค๊ณ  ํ–ˆ๋‹ค. cookie๋ฅผ ์„ค์ •ํ•˜๋Š” api๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ response ๋˜์—ˆ์Œ์—๋„ cookie ํƒญ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์—†์—ˆ๋‹ค. ๊ทธ๋ž˜์„œ, cookie ์„ค์ •์ด ์•ˆ๋˜์—ˆ๋‚˜ ํ–ˆ๋Š”๋ฐ postman cookie๊ด€๋ จ ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ๋ณด๋‹ˆ๊นŒ ์•„๋ž˜ ์‚ฌ์ง„์—์„œ Cookie๋ฅผ ํด๋ฆญํ•˜๋ผ๊ณ  ํ–ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์˜ค! ์ฟ ํ‚ค๊ฐ€ ์„ค์ •์ด ๋˜์—ˆ๊ตฌ๋‚˜ ํ•˜๊ณ , ๋‹ค๋ฅธ api๋ฅผ ๋ณด๋ƒˆ๋Š”๋ฐ req.cookies๊ฐ€ undefined๋ผ์„œ error๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค. [ํ•ด๊ฒฐ๋ฐฉ์•ˆ] ์•„๋ž˜ ์‚ฌ์ง„์—์„œ hidden ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด, ์ˆจ๊ฒจ์ ธ์žˆ๋˜ ์ •๋ณด๋“ค์ด ๋“ฑ์žฅํ•œ๋‹ค. ์—ฌ๊ธฐ ์•ˆ์— Cookie๊ฐ€ ๋“ค์–ด๊ฐ€ ์žˆ์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— req.cookies๊ฐ€ undefined์˜€๋˜ ๊ฒƒ์ด๋‹ค! postman์—..

Web 2021. 8. 27. 02:02