
// ๋๊ธฐ์ ๋น๋๊ธฐ
๋๊ธฐ : ํ์ฌ์ ์์๊ฐ ์คํ๋๊ณ ์ข
๋ฃ๋ ํ์ ๋ค์ ์คํ์ด ์ด๋ค์ง๋ค.
์์ ๋์ด๊ธฐ๊ตฌ : ์ด์ฐจ ํ๋์ ์น๊ฐ์ ํ์ฐ๊ณ ์ถ๋ฐํ๋ฉด
๋ค์ ์น๊ฐ์ ์์ ์ด์ฐจ๊ฐ ์ดํ์ ๋ง์น๊ณ ๋ค์ด์์ ์น๊ฐ๋ค์ด ๋ด๋ ค์ผ๋ง ํ์น์ด ๊ฐ๋ฅํ๋ค.
๊ทธ๋์ ๋๊ธฐ์ ์ด๋ค.
๋น๋๊ธฐ : ์คํ๋๋ ์์์ ์๊ด์์ด ๋จผ์ ์ฒ๋ฆฌ๋ ์์๋๋ก ๊ฒฐ๊ณผ๊ฐ์ด ์ถ๋ ฅ๋๋ค.
์์ ๋ง์ง : ๋ค์ด๊ฐ๋ ์์๋ ์ ์ฐฉ์์ธ ์์๋๋ก ๋ค์ด๊ฐ์ง๋ง
๋์ค๋ ์์๋ ์์ฌ๋ฅผ ๋นจ๋ฆฌ ๋ง์น ์์๋๋ก ๋์ค๊ธฐ ๋๋ฌธ์
๋น๋๊ธฐ ์ ์ด๋ค.
์คํ ์ํค๊ณ ์ ํ๋ ๊ธฐ๋ฅ
- API๋ฅผ ํตํด์ ํด๋น URI๋ก ์ ๊ทผํ์ฌ DB์ ์ ์ฅ๋์ด ์๋ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ๋ Read์ ๊ธฐ๋ฅ์ ๊ตฌํ์ํค๊ณ ์ ํ๋ค.
์ค๋ฅ ๋ฐ๊ฒฌ
router.get('/posts', (req, res) => {
const posts = Posts.find().sort({ createdAt: -1 });
res.status(200).json({ data: posts });
});
GET๋ฉ์๋๋ฅผ ํตํด์ localhost:3050 ํฌํธ์ /posts ์ฃผ์๋ก ์ ๊ทผํ๋ฉฐ,
Posts๋ผ๋ ๋ชจ๋ธ์์ find ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๊ณ , ์ฐพ์ ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ json ํํ๋ก ๋ฐ์๊ฒ์ด๋ค.
์ ์ฝ๋์์ ์ค๋ฅ ๋ฐ์.
Status: 500 Internal Server Error
TypeError: Converting circular structure to JSON
--> starting at object with constructor 'MongoClient'
| property 's' -> object with constructor 'Object'
| property 'sessionPool' -> object with constructor 'ServerSessionPool'
--- property 'client' closes the circle
status 500 ์ด๋ผ๋ ์ค๋ฅ ๋ฉ์์ง๋ ๋ณดํต ์ข ์์ฑ์ด๋ ๋ฆฌ์์ค ๋ถ์กฑ์ผ๋ก ์ธํด์ ๋ฐ์ํ๋ ์ค๋ฅ๋ผ๊ณ ํ๋ค.
์ด ๊ฐ์ ์ค๋ฅ ๋ฉ์์ง๋ JavaScript์์ ๋ฐ์ํ๋ ์ผ๋ฐ์ ์ธ ์ค๋ฅ์ด๋ฉฐ,
์ฃผ๋ก JSON.stringify()์ ๊ฐ์ ํจ์๋ฅผ ์ฌ์ฉํ ๋ ๋ฐ์ํ๊ณ ,
์ค๋ฅ์ ์์ธ์ JSON์ผ๋ก ๋ณํํ ์ ์๋ '์ํ์ฐธ์กฐ'๊ฐ ์์ ๋ ๋ฐ์ํ ์ ์๋ค.
์ด๋ฐ ์ค๋ฅ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด์๋ ์ํ์ฐธ์กฐ๋ฅผ ์ ๊ฑฐํ๋ฉด ์ฝ๊ฒ ํด๊ฒฐ์ด ๊ฐ๋ฅํ๋ค.
์ ์ฝ๋์ ๋ฐ์ดํฐ ํ๋ฆ
- ํด๋ผ์ด์ธํธ์์ api๋ฅผ ํตํด ์๋ฒ์๊ฒ db์์ posts์ ๋ฐ์ดํฐ ์กฐํ๋ฅผ ์์ฒญํ๊ณ ,
- ์๋ฒ๋ db์์ posts์ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ๋ค.
- ์กฐํํ ๋ฐ์ดํฐ๊ฐ ์๋ค๋ฉด ๊ทธ๊ฑธ db๋ก๋ถํฐ ์๋ฒ๋ ๋ฐ์์ค๊ณ ,
- ๋ฐ์์จ ๋ฐ์ดํฐ๋ฅผ ์ฐ๋ฆฌ์๊ฒ ๋ณด๋ด์ค๋ค.
์ด ๊ณผ์ ์์ ์ค๋ฅ๊ฐ ๋์ง์๊ณ , ์ฐ๋ฆฌ๊ฐ ์ํ๋ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ค๊ธฐ ์ํด์,
๊ผญ ํ์ํ ๊ณผ์ ์ ์๋ฒ๊ฐ db๋ก๋ถํฐ ์ ์ฒด ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ค๊ธฐ ์ ๊น์ง๋ ๋ค๋ฅธ ๋ก์ง์ด ์คํ์ด ๋์ด์๋ ์๋๋ค๋ ๊ฒ์ด๋ค.
๋ง์ธ ์ฆ์จ, ๋๊ธฐ์ ์ธ ์ฒ๋ฆฌ๊ฐ ํ์ํ ๋ถ๋ถ์ด๋ผ๋ ๋ป.
ํด๋ผ์ด์ธํธ๊ฐ ์๋ฒ์ ์์ฒญํ๋ ๋ถ๋ถ์ค์ db์์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ ๋ก์ง์ ์คํ์์ผ์ผ ํ๋ ๋ถ๋ถ์์๋ ํญ์ ๋ค์๊ณผ ๊ฐ์ ์ฝ๋๋ฅผ ์ผ๋์ ๋์ด์ผ๊ฒ ๋ค.
์ค๋ฅ ํด๊ฒฐ
router.get('/posts', async (req, res) => {
const posts = await Posts.find().sort({ createdAt: -1 });
res.status(200).json({ data: posts });
});
ํจ์ ์์์์ async๋ฅผ ์ฌ์ฉํจ์ผ๋ก์จ ๋๊ธฐ์ ์ผ๋ก ๋ณํํด์ฃผ๊ณ , ์ฐ๋ฆฌ๊ฐ ๋ฐ์ดํฐ๊ฐ ์ฒ๋ฆฌ๋๊ธฐ๊น์ง ๊ธฐ๋ค๋ ค์ผ ํ๋ ๋ถ๋ถ์ธ find๋ฉ์๋ ์์ await์ ๋ถ์์ผ๋ก์จ ์ด ์ฝ๋๋ฅผ ๋๊ธฐ์ ์ผ๋ก ๋ง๋ค์๋ค.
๊ฒฐ๊ณผ
Status: 200 OK
{
"data": [
{
"_id": "648b46da41f1e2639933139e",
"user": "์ ์ฌ์ฅ",
"password": 4,
"title": " ์ถ๊ฐ",
"content": "๊ฐ?",
"createdAt": "2023-06-15T17:14:02.746Z",
"__v": 0
},
{
"_id": "648b469f41537a61e0b930d9",
"user": "์ฅ์ฌ์ฅ",
"password": 3,
"title": "์์ฑ๋ ์ง ์ถ๊ฐ",
"content": " ์๋๊ฐ?",
"createdAt": "2023-06-15T17:13:03.663Z",
"__v": 0
},
{
"_id": "648b36b85b1ee47badb69cf7",
"user": "์ ์ฌ์ฅ",
"password": 2,
"title": "์์ฑ๋ ์ง ์ถ๊ฐ",
"content": "๋ด๋ฆผ์ฐจ์ ์ค์ ์ค",
"createdAt": "2023-06-15T16:05:12.788Z",
"__v": 0
},
{
"_id": "648b245829023c114601f315",
"user": "๋ฐ๊ฑฐ์ฑ",
"password": 1,
"title": "์์ฑ๋ ์ง ์ถ๊ฐ",
"content": "๊ฒฐ๊ณผ๋?",
"createdAt": "2023-06-15T14:46:48.146Z",
"__v": 0
}
]
}
์์๊ฐ์ด ๋๊ธฐ์ ์ผ๋ก ์ฒ๋ฆฌ๋ฅผ ํจ์ผ๋ก์จ ์ฐ๋ฆฌ๊ฐ ์ํ๋ ๋ฐ์ดํฐ๋ฅผ ์ ๊ฐ์ ธ์๊ณ , ์ฝ๋์ ๋ง๊ฒ ์ถ๋ ฅ์ด ๋๊ฒ์ ๋ณผ ์ ์๋ค.
#javascript #์ค๋ฅ #์ค๋ฅํด๊ฒฐ
#๋๊ธฐ_๋น๋๊ธฐ
#async_await
#get์์ฒญ
#Read_๊ตฌํ
#mongoose #mongodb