Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //...
- // feed from authors user is following
- router.get('/feed', auth.required, function(req, res, next){
- // configure the query params
- var limit = 20;
- var offset = 0;
- if(typeof req.query.limit !== 'undefined'){
- limit = req.query.limit;
- }
- if(typeof req.query.offset !== 'undefined'){
- offset = req.query.offset;
- }
- // grab user from mongo
- User.findById(req.payload.id).then(function(user){
- if (!user) { return res.sendStatus(401); }
- // find the paginated list of articles for authors we're following
- Promise.all([
- Article.find({ author: {$in: user.following}})
- .limit(Number(limit))
- .skip(Number(offset))
- .populate('author')
- .exec(),
- Article.count({ author: {$in: user.following}})
- ]).then(function(results){
- // return list of article JSON's
- var articles = results[0];
- var articlesCount = results[1];
- return res.json({
- articles: articles.map(function(article){
- return article.toJSONFor(user);
- }),
- articlesCount: articlesCount
- });
- }).catch(next);
- });
- });
- //...
Advertisement
Add Comment
Please, Sign In to add comment