'mongoose .skip() based on parameter value
I'm trying to implement pagination with mongoose for a mongoDB collection.
The console.log(req.params.start); correctly outputs the start value. However, the query does not start at this value — it starts from the beginning, regardless of the value.
If I change req.params.start to a non variable number, 2, for example — it correctly skips 2 records.
sampleRoute.route('/collection/:start').get((req, res) => {
console.log(req.params.start);
MyModel.find()
.skip(req.params.start)
.limit(2)
.exec(function (err, doc) {
if(err) { res.status(500).json(err); return; };
res.status(200).json(doc);
});
})
How can I successfully skip using a variable? Thank you.
Solution 1:[1]
As req.params.start is passed as a string, I needed to convert it to Number:
.skip(Number(req.params.start))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Joseph |
