Created
June 14, 2019 12:55
-
-
Save desaijay315/7a2f34765d608d85a6bf9f5ecb6887fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
router.get('/posts',authenticate, async (req,res) => { | |
//const _ispublished = req.query.published; | |
const match = {} | |
const sort = {} | |
if(req.query.published){ | |
match.published = req.query.published === 'true' | |
} | |
if(req.query.sortBy && req.query.OrderBy){ | |
sort[req.query.sortBy] = req.query.OrderBy === 'desc' ? -1 : 1 | |
} | |
try { | |
await req.user.populate({ | |
path:'posts', | |
match, | |
options:{ | |
limit: parseInt(req.query.limit), | |
skip: parseInt(req.query.skip), | |
sort | |
} | |
}).execPopulate() | |
res.send(req.user.posts) | |
} catch (error) { | |
res.status(500).send() | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment