Skip to content

Instantly share code, notes, and snippets.

@desaijay315
Created June 14, 2019 12:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save desaijay315/7a2f34765d608d85a6bf9f5ecb6887fc to your computer and use it in GitHub Desktop.
Save desaijay315/7a2f34765d608d85a6bf9f5ecb6887fc to your computer and use it in GitHub Desktop.
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