Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. router.get('/posts',authenticate, async (req,res) => {
  2. //const _ispublished = req.query.published;
  3. const match = {}
  4. const sort = {}
  5.  
  6. if(req.query.published){
  7. match.published = req.query.published === 'true'
  8. }
  9.  
  10. if(req.query.sortBy && req.query.OrderBy){
  11. sort[req.query.sortBy] = req.query.OrderBy === 'desc' ? -1 : 1
  12. }
  13.  
  14. try {
  15. await req.user.populate({
  16. path:'posts',
  17. match,
  18. options:{
  19. limit: parseInt(req.query.limit),
  20. skip: parseInt(req.query.skip),
  21. sort
  22. }
  23. }).execPopulate()
  24. res.send(req.user.posts)
  25. } catch (error) {
  26. res.status(500).send()
  27. }
  28. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement