Closed marufmax closed 5 years ago
Sorry for taking so long to reply.
I'm not sure I understand what your problem is. Are you saying you are seeing behavior like below?
go to next page
go to previous page
yea but go to previous page is: Items 18-19 it just loading one item
That sounds like a bug. Can you give me your query and example data that fails.
Also, make sure that when you call previousCursorPage(cursor)
, the passed cursor comes from pageInfo.previous
.
hope below exam make sense.
if (params) {
if (params.pagesize) {
query.limit(params.pagesize)
}
if (params.next) {
return query.cursorPage(params.next)
} else if (params.prev) {
return query.previousCursorPage(params.prev)
}
}
return query.cursorPage()
@marufmax did you resolve the problem?
this is how i am resolving this: Controller:
module.exports.getByCity = async (req, res) => {
let response;
if (req.query.next) {
response = await Weather.query()
.where('city', req.params.city)
.orderBy('id')
.cursorPage(req.query.next)
.limit(50);
} else if(req.query.back) {
response = await Weather.query()
.where('city', req.params.city)
.orderBy('id')
.previousCursorPage(req.query.back)
.limit(50);
} else {
response = await Weather.query()
.where('city', req.params.city)
.orderBy('id')
.cursorPage()
.limit(50);
}
response.pageInfo.nextPageUrl = req.path + '?next=' + response.pageInfo.next;
response.pageInfo.previousPageUrl = req.path + '?back=' + response.pageInfo.previous;
return res.json(response);
I believe there must be a simple way of doing this.
@marufmax you can simplify your code to
module.exports.getByCity = async (req, res) => {
const cursor = req.query.next !== undefined ? req.query.next : req.query.back;
const before = req.query.back !== undefined;
const response = await Weather.query()
.where('city', req.params.city)
.orderBy('id')
.cursorPage(cursor, before)
.limit(50);
response.pageInfo.nextPageUrl = req.path + '?next=' + response.pageInfo.next;
response.pageInfo.previousPageUrl = req.path + '?back=' + response.pageInfo.previous;
return res.json(response);
}
But I'm more interested in whether or not the code works as expected. Are you still getting only one item when going to previous page?
Edit: check for undefined instead of falsy.
closing as stale
Hi, When I pass previous cursor it only back to one item. Is there any workaround I can go to next limit?
Thanks