fknop / hapi-pagination

Hapi plugin to handle "custom" pagination
MIT License
63 stars 35 forks source link

The results must be an array #75

Closed Nickynodejs closed 5 years ago

Nickynodejs commented 5 years ago

"hapi": "^18.1.0", "hapi-pagination": "^2.1.0",Operation error reporting。

Debug: internal, implementation, error AssertionError [ERR_ASSERTION]: The results must be an array

wisdomQiuqiu commented 4 years ago

I have same problem。。。。how to resolve this problem。。。。"@hapi/hapi": "^18.4.0","hapi-pagination": "^3.0.0"

faustbrian commented 4 years ago

@wisdomQiuqiu What does your response look like? You should have something like return { results: [], totalCount: 10 } in your method.

wisdomQiuqiu commented 4 years ago

this is my code const { rows: results, count: totalCount } = await models.shops.findAndCountAll({ attributes: ["id", "name"], limit: request.query.limit, offset: (request.query.page - 1) * request.query.limit }); return { code: 200, content: { currentList: results, totalCount } };

wisdomQiuqiu commented 4 years ago

What causes this problem??

faustbrian commented 4 years ago

Based on that code it should look more like this.

const { rows: results, count: totalCount } = await models.shops.findAndCountAll({
    attributes: ["id", "name"],
    limit: request.query.limit,
    offset: (request.query.page - 1) * request.query.limit
});

return { results, totalCount };

The results and totalCount properties have to be at the root of the response.

wisdomQiuqiu commented 4 years ago

How do i want to nest one more layer??

faustbrian commented 4 years ago

You can't but you can add custom properties. Just read the documentation for https://github.com/fknop/hapi-pagination#hpaginatearrayobject-totalcount-options--.

wisdomQiuqiu commented 4 years ago

ok,thank you