Reputation: 563
I have an object that looks like this:
Topic
{
"posts": [
"5baabfeb87a1401432791534",
"5bac21814a0f9a2262a77f1b"
],
"_id": "5ba06e74dbc05f039490438c",
"topic": "new topic",
"description": "this is the description",
"date": "2018-09-18T03:18:12.062Z",
"__v": 2
}
im trying to return Topic.posts as an array of records using mongoose's populate. this is what i'm trying.
router.get('/:topicId/posts', (req,res) => {
const {topicId} = req.params
Topic.findById(topicId)
.then(topic => res.json(topic.posts.populate('posts'))
)
.catch(err => res.send(err))
});
//getting a {} blank object its not even an array?
if possible id like it to return the whole topic object with array populated.
Topic
{
"posts": [
{
"_id": "5bac21814a0f9a2262a77f1b",
"post": "post 2",
"description": "blah blah",
},
{
"_id": "5baabfeb87a1401432791534",
"post": " this is a post",
"description": "blah blah blah"
}
],
"_id": "5ba06e74dbc05f039490438c",
"topic": "new topic",
"description": "this is the description",
"date": "2018-09-18T03:18:12.062Z",
"__v": 2
}
what are my options?
Upvotes: 0
Views: 23
Reputation: 563
figured it out
router.get('/:topicId/posts', (req,res) => {
const {topicId} = req.params
Topic.findById(topicId) //I thought you had to map the array but populate iterates for you
.populate('posts')
.then(topic => res.json(topic))
.catch(err => res.send(err))
});
Upvotes: 1