Basic pagination working
This commit is contained in:
@@ -78,7 +78,7 @@
|
||||
|
||||
router.get('/search/listings', function () {
|
||||
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(req, res, next) {
|
||||
var bounds, minPrice, maxPrice, minSize, maxSize, rooms, adType, category, sort, lastRecordId, properties, query, _bounds$split$map, _bounds$split$map2, lat1, lng1, lat2, lng2, box, price, and, allRooms, or, size, allCategories, _or, cnt, all;
|
||||
var bounds, minPrice, maxPrice, minSize, maxSize, rooms, adType, category, sort, page, properties, query, _bounds$split$map, _bounds$split$map2, lat1, lng1, lat2, lng2, box, price, and, allRooms, or, size, allCategories, _or, cnt, all;
|
||||
|
||||
return regeneratorRuntime.wrap(function _callee$(_context) {
|
||||
while (1) {
|
||||
@@ -94,7 +94,7 @@
|
||||
adType = req.query.adType;
|
||||
category = req.query.category;
|
||||
sort = req.query.sort;
|
||||
lastRecordId = req.query.lastRecordId;
|
||||
page = req.query.page || 0;
|
||||
properties = db.collection('listings');
|
||||
query = {};
|
||||
|
||||
@@ -204,21 +204,21 @@
|
||||
|
||||
res.header('X-Total-Count', cnt);
|
||||
|
||||
if (lastRecordId) {
|
||||
query = Object.assign(query, {
|
||||
"_id": {
|
||||
"$gt": new ObjectID(lastRecordId)
|
||||
}
|
||||
});
|
||||
}
|
||||
//if (lastRecordId) {
|
||||
//query = Object.assign(query, {
|
||||
//"_id": {
|
||||
//"$gt": new ObjectID(lastRecordId)
|
||||
//}
|
||||
//});
|
||||
//}
|
||||
|
||||
_context.next = 29;
|
||||
_context.next = 28;
|
||||
return properties.find(query, {
|
||||
//"sort": [['field1','asc'], ['field2','desc']]
|
||||
"sort": [['price', 'asc']]
|
||||
}).limit(20).toArray();
|
||||
}).skip(20 * page).limit(20).toArray();
|
||||
|
||||
case 29:
|
||||
case 28:
|
||||
all = _context.sent;
|
||||
|
||||
|
||||
@@ -228,22 +228,22 @@
|
||||
|
||||
res.json(all);
|
||||
res.end();
|
||||
_context.next = 39;
|
||||
_context.next = 38;
|
||||
break;
|
||||
|
||||
case 35:
|
||||
_context.prev = 35;
|
||||
case 34:
|
||||
_context.prev = 34;
|
||||
_context.t0 = _context['catch'](0);
|
||||
|
||||
console.log('error:', _context.t0);
|
||||
next(_context.t0);
|
||||
|
||||
case 39:
|
||||
case 38:
|
||||
case 'end':
|
||||
return _context.stop();
|
||||
}
|
||||
}
|
||||
}, _callee, undefined, [[0, 35]]);
|
||||
}, _callee, undefined, [[0, 34]]);
|
||||
}));
|
||||
|
||||
return function (_x, _x2, _x3) {
|
||||
@@ -256,8 +256,8 @@
|
||||
|
||||
app.use(function (req, res, next) {
|
||||
res.header("Access-Control-Allow-Origin", "*");
|
||||
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, X-Last-Record-Id");
|
||||
res.header("Access-Control-Expose-Headers", "X-Last-Record-Id");
|
||||
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, X-Last-Record-Id, X-Total-Count");
|
||||
res.header("Access-Control-Expose-Headers", "X-Last-Record-Id, X-Total-Count");
|
||||
res.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
|
||||
res.header('Access-Control-Allow-Credentials', 'true');
|
||||
next();
|
||||
|
||||
@@ -25,7 +25,8 @@ router.get('/search/listings', async (req, res, next) => {
|
||||
const adType = req.query.adType;
|
||||
const category = req.query.category;
|
||||
const sort = req.query.sort;
|
||||
const lastRecordId = req.query.lastRecordId;
|
||||
const page = req.query.page || 0;
|
||||
|
||||
const properties = db.collection('listings');
|
||||
let query = {};
|
||||
|
||||
@@ -123,18 +124,18 @@ router.get('/search/listings', async (req, res, next) => {
|
||||
|
||||
res.header('X-Total-Count', cnt);
|
||||
|
||||
if (lastRecordId) {
|
||||
query = Object.assign(query, {
|
||||
"_id": {
|
||||
"$gt": new ObjectID(lastRecordId)
|
||||
}
|
||||
});
|
||||
}
|
||||
//if (lastRecordId) {
|
||||
//query = Object.assign(query, {
|
||||
//"_id": {
|
||||
//"$gt": new ObjectID(lastRecordId)
|
||||
//}
|
||||
//});
|
||||
//}
|
||||
|
||||
const all = await properties.find(query, {
|
||||
//"sort": [['field1','asc'], ['field2','desc']]
|
||||
"sort": [['price','asc']]
|
||||
}).limit(20).toArray();
|
||||
}).skip(20 * page).limit(20).toArray();
|
||||
|
||||
if (all.length > 0) {
|
||||
res.header('X-Last-Record-Id', [...all].pop()._id);
|
||||
@@ -154,8 +155,8 @@ app.use(bodyParser.json());
|
||||
|
||||
app.use(function(req, res, next) {
|
||||
res.header("Access-Control-Allow-Origin", "*");
|
||||
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, X-Last-Record-Id");
|
||||
res.header("Access-Control-Expose-Headers", "X-Last-Record-Id");
|
||||
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, X-Last-Record-Id, X-Total-Count");
|
||||
res.header("Access-Control-Expose-Headers", "X-Last-Record-Id, X-Total-Count");
|
||||
res.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
|
||||
res.header('Access-Control-Allow-Credentials', 'true');
|
||||
next();
|
||||
|
||||
Reference in New Issue
Block a user