2016-06-25 45 views
0

我有一个分页设置,其中我的订阅源与索引路径路径/一起显示,然后使用/feed/:pageNumber访问其他页面。我没有提供下一个和上一个记录的分页问题,​​但是当我想要在最后一个上一页单击时重新定向到/路径,因为这有最新的记录。我试图在else语句中使用res.redirect('/'),但我得到一个错误Error: Can't set headers after they are sent.。有没有更好的方法在上次上一页点击时重定向到家中?Expressjs分页重定向

E.x.用户在'/'上,点击下一步并发送到'/ feed/2'。当用户单击Previous时,它们应该被带回'/'。如果他们在/ feed/3,4,5等等/那么它会使用户比当前参数值小1。

节我试图修复:

if(req.params.pageNumber > 2){ 
        res.locals.previous = true; 
        res.locals.previousPage = req.params.pageNumber - 1; 
       } else { 
        res.locals.previous = true; 
        res.locals.previous = res.redirect('/'); 
       } 

查看:

<!DOCTYPE html> 
<head> 
    {{> app/app-head}} 
</head> 
<body> 
    {{> app/app-navigation}} 

    <div class="container"> 
     <h1 class="page-title-header">Activity Feed</h1> 
      {{> app/card}} 
    </div> 
    {{#if previous}} 
    <a href="/feed/{{previousPage}}">Previous Page</a> 
    {{/if}} 
    {{#if secondPage}} 
    <a href="/feed/{{secondPage}}">Next Page</a> 
    {{/if}} 
    {{#if next}} 
    <a href="/feed/{{nextPage}}">Third Page</a> 
    {{/if}} 
</body> 

路线:

/*==== /====*/ 

appRoutes.route('/') 

    .get(function(req, res){ 

     models.Card.findAll({ 
      order: 'cardDate DESC', 
      include: [{ 
       model: models.User, 
       where: { organizationId: req.user.organizationId }, 
       attributes: ['organizationId', 'userId'] 
      }], 
      limit: 10 
     }).then(function(card){ 

      function feedLength(count){ 
       if (count >= 10){ 
        return 2; 
       } else { 
        return null; 
       } 
      }; 

      res.render('pages/app/high-level-activity-feed.hbs',{ 
       card: card, 
       user: req.user, 
       secondPage: feedLength(card.length) 
      }); 
     }); 
    }) 

    .post(function(req, res){ 
     models.Card.create({ 
      card: req.body.cardDate, 
      userId: req.user.userId 
     }).then(function() { 
      res.redirect('/app'); 
     }).catch(function(error){ 
      res.send(error); 
     }) 
    }); 

appRoutes.route('/feed/:pageNumber') 

    .get(function(req, res){ 

     function paginationPage(count){ 
      if(count == 2){ 
       return 10; 
      } else { 
       return (count - 1) * 10; 
      } 
     }; 
     var skip = parseInt(req.params.pageNumber); 

     models.Card.findAll({ 
      order: 'cardDate DESC', 
      include: [{ 
       model: models.User, 
       where: { organizationId: req.user.organizationId }, 
       attributes: ['organizationId', 'userId'] 
      }], 
      offset: paginationPage(skip), 
      limit: 10 
     }).then(function(annotation){ 
       if(annotation.length == 10){ 

        res.locals.next = true; 
        res.locals.nextPage = parseInt(req.params.pageNumber) + 1; 
        console.log('This is the next page pagination: ' + res.locals.nextPage); 
       } 

       if(req.params.pageNumber > 2){ 
        res.locals.previous = true; 
        res.locals.previousPage = req.params.pageNumber - 1; 
       } else { 
        res.locals.previous = true; 
        res.locals.previous = res.redirect('/'); 
       } 
      res.render('pages/app/high-level-activity-feed.hbs',{ 
       card: card, 
       user: req.user 
      }); 
     }); 
    }) 

回答

1

的问题是双重的:

  • 模板始终前缀与/feed/上一页链接:

    <a href="/feed/{{previousPage}}">Previous Page</a> 
    
  • res.redirect('/')从Express服务器内执行实际重定向(它不会产生一个链接,或客户端的JavaScript)

一个可能的解决方案是将第三状态添加到您的模板:

{{#if backHome}} 
<a href="/">Previous Page</a> 
{{/if}} 

而在你的服务器代码:

if (req.params.pageNumber > 2) { 
    res.locals.previous = true; 
    res.locals.previousPage = req.params.pageNumber - 1; 
} else { 
    res.locals.backHome = true; 
} 
+0

这工作!感谢@robertklep的帮助和解释。 – cphill