fknop / hapi-pagination

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

Error when setting page name #13

Closed msaron closed 8 years ago

msaron commented 8 years ago

I downloaded the plugin and the tests all ran fine. If I use the default values, the plugin works great. However, when I change the page name to 'myPage', I get an error. I have the plugin configuration in my glue manifest as:

{ plugin: { register: 'hapi-pagination', options: { query: { page: { name: 'myPage' } }, routes: { include: ['/world/city'] } } } }

The error I get is the following: `/home/mana/node-apps/mana/_mana-hapi/node_modules/hapi-pagination/node_modules/hoek/lib/index.js:736 throw new Error(msgs.join(' ') || 'Unknown error'); ^

Error: The results must be an array at Object.exports.assert (/home/mana/node-apps/mana/_mana-hapi/node_modules/hapi-pagination/node_modules/hoek/lib/index.js:736:11) at internals.onPreResponse (/home/mana/node-apps/mana/_mana-hapi/node_modules/hapi-pagination/lib/ext.js:143:14) at Items.serial (/home/mana/node-apps/mana/_mana-hapi/node_modules/hapi/lib/request.js:402:22) at iterate (/home/mana/node-apps/mana/_mana-hapi/node_modules/hapi/node_modules/items/lib/index.js:36:13) at Object.exports.serial (/home/mana/node-apps/mana/_mana-hapi/node_modules/hapi/node_modules/items/lib/index.js:39:9) at _protect.run (/home/mana/node-apps/mana/_mana-hapi/node_modules/hapi/lib/request.js:397:15) at internals.Protect.run (/home/mana/node-apps/mana/_mana-hapi/node_modules/hapi/lib/protect.js:64:5) at internals.Request._invoke (/home/mana/node-apps/mana/_mana-hapi/node_modules/hapi/lib/request.js:395:19) at internals.Request._reply (/home/mana/node-apps/mana/_mana-hapi/node_modules/hapi/lib/request.js:454:10) at Items.serial (/home/mana/node-apps/mana/_mana-hapi/node_modules/hapi/lib/request.js:388:21) at done (/home/mana/node-apps/mana/_mana-hapi/node_modules/hapi/node_modules/items/lib/index.js:31:25) at finalize (/home/mana/node-apps/mana/_mana-hapi/node_modules/hapi/lib/handler.js:21:16) at Hoek.once (/home/mana/node-apps/mana/_mana-hapi/node_modules/hapi/lib/protect.js:52:16) at wrapped (/home/mana/node-apps/mana/_mana-hapi/node_modules/hapi/node_modules/hoek/lib/index.js:867:20) at finalize (/home/mana/node-apps/mana/_mana-hapi/node_modules/hapi/lib/handler.js:86:16) at wrapped (/home/mana/node-apps/mana/_mana-hapi/node_modules/hapi/node_modules/hoek/lib/index.js:867:20) `

If I remove the query option, then the plugin works fine.

fknop commented 8 years ago

That's weird, the results must be an array is when the plugin cannot find an array as your results. Could you paste me the code for your route /world/city ?

msaron commented 8 years ago

Here you go. Weird thing is it works perfectly fine if I don't set the query options for pagination.

get() { const limit = parseInt(this.request.query.limit) || 25; const page = parseInt(this.request.query.page) || 1; const pagination = this.request.query.pagination; const offset = limit * (page - 1); const queryParams = Object.assign({}, this.request.query); delete queryParams.limit; delete queryParams.page; delete queryParams.pagination;

const p1 = this.knex(this.table).count().first().where(queryParams);
const p2 = this.knex(this.table).select().where(queryParams).limit(limit).offset(offset);

Promise.all([p1, p2]).then(values => {
  const totalCount = parseInt(values[0].count) || 0;
  this.reply.paginate(values[1], totalCount);
}).catch(err => this.reply(err.message));

}

msaron commented 8 years ago

Attached is the full code file for the model that gets the data. I had to change the extension from .js to .txt otherwise github would not let me load the file.

city.txt

fknop commented 8 years ago

Why are you deleting your attributes from queryParams ? Also, if you set the name of the page to myPage, it will be available in request.query.myPage and not request.query.page. And the plugin can also handle defaults values for your query parameters, no need to parseInt() yourself.

msaron commented 8 years ago

Yes, it works if I take out the where(queryParams) from the knex sql. Looks like myPage was being inserted into the query and knex was not returning any results so hapi-pagination was saying that the results should be an array.

Thank you!!!