I found an example on bitbucket of paginating the results of a query, but it was written in java script, and I'm having a little trouble declaring the types.
and getting this error on this line
const pagination:TPagination = usersResults.toJSON()
The type '{ meta: any; data: ModelObject[]; }' does not have the following properties of type
***sample code****
async index ({ view, params, request, response }) {
const page = params.page || 1
const search = request.input('search') || ''
const employees = await Employee.query()
.where('name', 'LIKE', '%' + search + '%')
.paginate(page, 10)
const pagination = employees.toJSON()
pagination.route = 'employees.pagination'
if(pagination.lastPage < page && page != 1) {
response.route(pagination.route, { page: 1 }, null, true)
}
else {
pagination.offset = (pagination.page - 1) * pagination.perPage
pagination.search = search
return view.render('employees.index', { employees: pagination })
}
}
}
***my code***
public async searchByName ({request,params, response}: HttpContextContract) {
interface TPagination {
route: string;
lastPage: number;
offset:number;
search:string;
page:number;
perPage:number;
[key: string]: any;
meta: any;
data: ModelObject[];
}
try {
const page = params.page || 1
const search = request.input('search') || ''
const usersResults = await User.query()
.where('name', 'LIKE', '%' + search + '%')
.orWhere('cns','LIKE','%'+ search +'%')
.paginate(page, 10)
const pagination:TPagination = usersResults.toJSON()
pagination.route = 'pagination.users'
if(pagination.lastPage < page && page != 1) {
response.redirect().toRoute(pagination.route, { page: 1 })
}
else {
pagination.offset = (pagination.page - 1) * pagination.perPage
pagination.search = search
}
} catch {
return response.status(400).json('{"error":"Usuário não encontrado!"}');
}
}