node-restful icon indicating copy to clipboard operation
node-restful copied to clipboard

To get total records count for pagination

Open diptenagile opened this issue 9 years ago • 1 comments

I found a solution for get total record count that would be helpful in pagination. You will get executed query in req.quer variable . i removed options which would remove skip and limit from query after that re-applied that query. I write following code inside the after hook.

Resource.after('get', function(req, res, next) {
    delete req.quer.options;    // Remove skip and limit to get total count
    req.quer.count(function(err, totalRecords) { //Apply remaining query
          if (err) {
            console.log(err);
                next();
          }else {
            console.log("Total records", totalRecords);
            next();
          }
     }); 

});

Hope that would be helpful in pagination.

diptenagile avatar Sep 13 '16 11:09 diptenagile

Thanks for this, unfortunately we cannot send headers from hooks, because Express explodes.

matheo avatar Mar 10 '17 03:03 matheo