3

Here is the code that I am using in meanstack to get the limited data

apiRouter.get('/pagination_posts', function(req, res){
    console.log(req.params)       // getting object having value for limit and offset
    Post.count({},function(err,count){
        console.log(count)     // total number of records
        Post.find({}, function(err, posts){
            if (err) res.send(err);
            res.json({total:count,posts:posts});
        }).skip(req.query.offset).limit(req.query.limit);
    });
});

Getting following error:

events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11)
    at ServerResponse.header (/Volumes/E/simerjit/fwrkdeploy/node_modules/express/lib/response.js:718:10)
    at ServerResponse.send (/Volumes/E/simerjit/fwrkdeploy/node_modules/express/lib/response.js:163:12)
    at ServerResponse.json (/Volumes/E/simerjit/fwrkdeploy/node_modules/express/lib/response.js:249:15)
    at /Volumes/E/simerjit/fwrkdeploy/server/api/posts.js:29:9
    at /Volumes/E/simerjit/fwrkdeploy/node_modules/mongoose/lib/model.js:3822:16
    at /Volumes/E/simerjit/fwrkdeploy/node_modules/kareem/index.js:213:48
    at /Volumes/E/simerjit/fwrkdeploy/node_modules/kareem/index.js:131:16
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickCallback (internal/process/next_tick.js:104:9)

If I am using static values here }).skip(0).limit(10);, it is working fine, but I want to use this api for pagination, so need to pass dynamic limit and offset.

1
  • 1
    if (err) res.send(err); It's not like invoking a callback or promise resolve/reject where it branches out. It actually allows the next line of code to execute. So instead either use else or return i.e if (err) return res.send(err); to stop further execution. Then of course you need to fix your other error. Note that you are not checking the error status on .count(). You really should. You really should be using promises and chaining, or better yet async/await and a try..catch block. Commented Sep 1, 2017 at 13:15

1 Answer 1

1

you have to stop your async code using return keyword or handle the proper condition flow will slove you issue {I am using return below}

  apiRouter.get('/pagination_posts', function(req, res){
        console.log(req.params)       // getting object having value for limit and offset
        Post.count({},function(err,count){
            console.log(count)     // total number of records
            Post.find({}, function(err, posts){
                if (err) return res.json(err);  
               return res.json({total:count,posts:posts});
            }).skip(req.query.offset).limit(req.query.limit);
        });
    });

other wise maintain the Condition controll flow

apiRouter.get('/pagination_posts', function(req, res){
            console.log(req.params)       // getting object having value for limit and offset
            Post.count({},function(err,count){
                console.log(count)     // total number of records
                Post.find({}, function(err, posts){
        if (err) ? res.json(err):  res.json({total:count,posts:posts});
                }).skip(req.query.offset).limit(req.query.limit);
            });
        });
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.