--- tags: Node.js 直播班 - 2022 春季班 --- # 🏅 Day 41 ## 取得所有貼文,加上 comment ### 題目(將答案寫在 HackMD 並提交至回報區) **使用 populate() 關聯 comments 欄位,取得所有貼文時需顯示出該貼文的所有留言** 回報流程 --- 請同學依照下圖教學觀看解答、回報答案:  回報格式:請在「回報區」貼上 CodePen 或 HackMD 連結回報答案 (為了統計人數,請同學依序加上「報數」) <!-- 解答 範例參考 https://github.com/gonsakon/express-week4-sample/blob/week8/routes/posts.js#L9-L23 ```javascript= routes/posts.js router.get('/', async function(req, res, next) { const timeSort = req.query.timeSort == "asc" ? "createdAt":"-createdAt" const q = req.query.q !== undefined ? {"content": new RegExp(req.query.q)} : {}; const post = await Post.find(q).populate({ path: 'user', select: 'name photo ' }).populate({ path: 'comments', select: '-_id -post comment user' }).sort(timeSort); res.status(200).json({ status: 'success', post }) }); ``` --> 回報區 --- | 報數 | 組別 / 名字 | codepen / hackMD / 其他回饋 | | ---- | ----------- | ------------------ | |1| 第 14 組|East | [hackMD](https://hackmd.io/EBXW3wscSpG1yuirdHNtIg) | |2| 第 11 組|Han Lai | [hackMD]([https://hackmd.io/EBXW3wscSpG1yuirdHNtIg](https://hackmd.io/mvl_fVvVRkOgd6mp6vhJ0A?view)) | |3| 第 16 組|皓皓 | [hackMD](https://hackmd.io/@cutecat8110/SyKUtWEic) |
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up