1
const limit = +res.query.limit || 20 // or by default 20 items
const page = +req.query.page || 1
Events = await EventModels.find({}).sort('-created').skip(limit * page).limit(limit).exec()

I have 5 records, but I don't get anything when I passed limit to be equal to 100, now I'm confused now the limit work, I expect to see all of my 5 records when I do .limit(100) or I messed up with the skip?

I followed an answer here https://stackoverflow.com/a/14822142/9728810

  • `(limit * (page -1))`. Otherwise it's `20 - 20` which is `0` Remember `n-1`. This really should be ingrained as computer science 101 – Neil Lunn May 15 '18 at 09:40
  • where should I put that? assign to limit const?? –  May 15 '18 at 09:43
  • Quoting the comment under the linked answer in your question `let page = (Math.abs(req.query.page) || 1) - 1`. Which is the same `n-1` I just mentioned. Technically the "answer" is incorrect, but the "comment' corrects it. – Neil Lunn May 15 '18 at 09:43

0 Answers0