--- tags: Node.js 直播班 - 2023 春季班 --- # 🏅 Day 40 - 取得所有貼文,加上 comment ## 題目 **使用 populate() 關聯 comments 欄位,取得所有貼文時需顯示出該貼文的所有留言** ## 回報流程 將答案寫在 CodePen 並複製 CodePen 連結貼至底下回報就算完成了喔! 解答位置請參考下圖(需打開程式碼的部分觀看)  <!-- 解答: 範例參考 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 }) }); ``` --> 回報區 --- | 報數 | 組別/Discord 名字 | Codepen/HackMD/其他回饋 | |:----:|:-----------------------:|:-------------------------------------------------------------------------:| | 1 | 中 4 組 / jimkk159 | [HackMD - Day 40](https://hackmd.io/U1o3F-xNQ0GxpiheWA8HUQ) |
×
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