fknop / hapi-pagination

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

Metadata not present in result when using location: 'body' #48

Closed ambrons closed 7 years ago

ambrons commented 7 years ago

I've been trying to get the meta data to show up in the result when using the formation:

request.totalCount = totalCount;
return reply(results);

This yields the results as an array by itself instead of wrapped in an object with { meta:..., results:... }. I've traced through the ext.js file and over the onPostHandler function. the response just prior to reply.continue(response) produces the right result, however what the client gets is wrong.

Actually I've converted the reply.continue(response) to a reply(response, just for testing and it works as expected. I know that reply.continue is suppose to be the way to go with pre and post handlers. I've also checked and I don't see to have any other onPostHandler that could be causing the issue that I can tell.

I'm using multiselection, but that doesn't see to be the issue. Also switching meta.location = 'header' produces the correction headers for the next, prev, etc links. However I want them in the body.

I'm using glue as an FYi. I've included the snippets that might help:

Glue Yaml - Plugin Configuration

-
    plugin:
      register: hapi-pagination
      options:
        routes:
          include: []
        meta:
          baseUri: ''
    options:
      routes:
        prefix: '/auth'
      select: ['private', 'public']

Route Configuration and Handler

handler: function (request, reply) {

        const PrincipalStore = request.server.plugins.models.Principal;
        const { limit, page } = request.query;

        return Promise.all([
            PrincipalStore.find({}).limit(limit).skip(page * limit).sort({ name: 'asc' }).exec()
            .then((principals) => {

                return principals.map((principal) => principal.toObject({ transform: Utils.transformPrincipal }));
            }),
            PrincipalStore.count().exec(),
        ]).then(([results, totalCount]) => {

            request.totalCount = totalCount;
            return reply(results);
        }).catch((reason) => reply(Boom.badRequest(reason)));
    },
       pagination: {
            enabled: true,
        },
    },

The line referenced above in question: https://github.com/fknop/hapi-pagination/blob/master/lib/ext.js#L250

The request seems to be working fine:

170603/043041.261, [response,api] http://platinum:3009: get /auth/principals {"limit":5,"page":1} 200 (61ms)

I've also tried switch from request.totalCount to

reply.paginate(results, totalCount)

This ends up producing an object like so:

{
    response: null,
    results: [ /* my data */],
    totalCount: /*total count*/
}
ambrons commented 7 years ago

So it turns out I wasn't doing anything wrong. My libraries were a bit out of data. I was using Glue v3.3.1 and Hapi v15.x. After updating both of those to the latest it just started working.

I knew the code looked right on both ends, but for the life of me couldn't figure out why it wasn't working. My guess is there was a bug in onPostHandler in Hapi or perhaps in glue as it tries to combine event handlers that was fixed since I last installed.

I've created an example project that shows the technology I use working with hapi-pagination. It's available here: https://github.com/ambrons/hapi-pagination-example

fknop commented 7 years ago

That's good to know!

Thanks for the repository, I'll take a look.

ambrons commented 7 years ago

Thanks, If I get some more time I might track down when and what exactly fixed it.