Skip to content

Commit

Permalink
Fix paging parameter (page/skip/limit) handling. (openhab#245)
Browse files Browse the repository at this point in the history
* Fix paging parameter (page/skip/limit) handling.

Query parameters are present in req.query, req.params only contains
named parameters that are part of the actual URL.

This re-applies the changes of commit
34a09b6 which were accidentally dropped
in commit cea8d8f.

Signed-off-by: Danny Baumann <[email protected]>

* Convert limit/skip/page parameters to integers before using them.

In particular, MongoDB complains when getting passed limit or skip as
strings.

Signed-off-by: Danny Baumann <[email protected]>
  • Loading branch information
maniac103 authored and digitaldan committed Mar 2, 2019
1 parent 90cb738 commit 6302628
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 15 deletions.
6 changes: 3 additions & 3 deletions routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ var moment = require('moment');
var system = require('../system');

exports.notificationsget = function(req, res) {
var limit = req.params.limit > 0 ? req.params.limit : 10,
skip = req.params.skip > 0 ? req.params.skip : 0;
var limit = req.query.limit > 0 ? parseInt(req.query.limit) : 10,
skip = req.query.skip > 0 ? parseInt(req.query.skip) : 0;
Notification.find({user: req.user.id}, '-user')
.limit(limit)
.skip(skip)
Expand All @@ -34,4 +34,4 @@ exports.notificationssettingsget = function(req, res) {
};
}
res.send(config);
};
};
12 changes: 6 additions & 6 deletions routes/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ var moment = require('moment');

exports.eventsget = function(req, res) {
var perPage = 20,
page = req.params.page > 0 ? req.params.page : 0;
page = req.query.page > 0 ? parseInt(req.query.page) : 0;
req.user.openhab(function(error, openhab) {
if (!error && openhab != null) {
var filter = {openhab: openhab};
if (req.params.source)
filter.source = req.params.source;
if (req.query.source)
filter.source = req.query.source;
Event.find(filter)
.limit(perPage)
.skip(perPage * page)
Expand All @@ -20,7 +20,7 @@ exports.eventsget = function(req, res) {
.exec(function(error, events) {
Event.count().exec(function (err, count) {
res.render('events', { events: events, pages: count / perPage, page: page,
title: "Events", user: req.user, openhab: openhab, source: req.params.source,
title: "Events", user: req.user, openhab: openhab, source: req.query.source,
errormessages:req.flash('error'), infomessages:req.flash('info') });
});
});
Expand All @@ -33,7 +33,7 @@ exports.eventsget = function(req, res) {
exports.eventsvaluesget = function(req, res) {
req.user.openhab(function(error, openhab) {
if (!error && openhab != null) {
var filter = {openhab: openhab, source: req.params.source};
var filter = {openhab: openhab, source: req.query.source};
Event.find(filter).sort({when: 'asc'}).select('when status -_id').exec(function(error, events) {
if (!error) {
var result = [];
Expand All @@ -47,4 +47,4 @@ exports.eventsvaluesget = function(req, res) {
});
}
});
}
}
2 changes: 1 addition & 1 deletion routes/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var Openhab = require('../models/openhab');
var Item = require('../models/item');

exports.itemsget = function(req, res) {
switch (req.params.sort) {
switch (req.query.sort) {
default:
case "name":
var sortValue = {name: 'asc'};
Expand Down
2 changes: 1 addition & 1 deletion routes/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var moment = require('moment');

exports.notificationsget = function(req, res) {
var perPage = 20,
page = req.params.page > 0 ? req.params.page : 0;
page = req.query.page > 0 ? parseInt(req.query.page) : 0;
req.user.openhab(function(error, openhab) {
if (!error && openhab != null) {
var filter = {user: req.user.id};
Expand Down
8 changes: 4 additions & 4 deletions routes/staff.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var redis = require('../redis-helper');

exports.staffget = function(req, res) {
var perPage = 20,
page = req.params.page > 0 ? req.params.page : 0;
page = req.query.page > 0 ? parseInt(req.query.page) : 0;
req.user.openhab(function(error, openhab) {
if (!error && openhab != null) {
var filter = {invited: null};
Expand Down Expand Up @@ -78,7 +78,7 @@ exports.processenroll = function(req, res) {

exports.invitationsget = function(req, res) {
var perPage = 20,
page = req.params.page > 0 ? req.params.page : 0;
page = req.query.page > 0 ? parseInt(req.query.page) : 0;
req.user.openhab(function(error, openhab) {
if (!error && openhab != null) {
if (req.query.hasOwnProperty('email')) {
Expand Down Expand Up @@ -142,7 +142,7 @@ exports.deleteinvitation = function(req, res) {

exports.oauthclientsget = function(req, res) {
var perPage = 20,
page = req.params.page > 0 ? req.params.page : 0;
page = req.query.page > 0 ? parseInt(req.query.page) : 0;
req.user.openhab(function(error, openhab) {
if (!error && openhab != null) {
OAuth2Client.find()
Expand All @@ -160,4 +160,4 @@ exports.oauthclientsget = function(req, res) {

}
});
}
}

0 comments on commit 6302628

Please sign in to comment.