1
我有一個論壇,有3個集合的數據庫:線程,帖子,評論。 我有一個GET請求返回的個人論壇主題是填充每個線程與用戶的帖子,每個用戶都交與它所做的這些工作有任何意見,如下圖所示:返回Mongoose中填充對象的數組
router.get('/:id', (req, res) => {
Threads
.findById(req.params.id)
.lean()
.populate({path: 'posts'})
.exec(function(err, docs){
var options = {
path: 'posts.comments',
model: 'comments'
};
if(err) return res.json(500);
Threads.populate(docs, options, function(err, thread){
res.json(thread);
})
})
})
當這GET請求是由它會返回一個論壇主題,像這樣:
{
"_id": "5924ad549a08ed4e70a9c89f",
"title": "Testing Full Schemas",
"author": "Mongoose",
"content": "Schema Content",
"posts": [
{
"_id": "5924ad999a08ed4e70a9c8a0",
"content": "New Schema Post",
"user": "Mongodb",
"comments": [
{
"_id": "5924ae489a08ed4e70a9c8a1",
"comment": "New Schema Content",
"user": "Matt",
"likes": 0,
"created": "2017-05-25T12:41:58.319Z"
}
]
}
現在我需要一個GET請求返回的ALL線程,每個線程職位的數組(router.get(「/」))和評論將被填充。我試圖取代:
Threads
.findById(req.params.id)
與
Threads
.find(req.params.id)
,但它無法正常工作。有沒有人有這樣的想法可以完成?