hapijs / hapi

The Simple, Secure Framework Developers Trust
https://hapi.dev
Other
14.63k stars 1.34k forks source link

Error on implementing cache system with @hapi 21.3.2 #4464

Closed sylvainJacot closed 1 year ago

sylvainJacot commented 1 year ago

Support plan

Context

How can we help?

I've been asked to upgrade our hapi server from version 11 to the latest version (21.3.2), which requires quite a bit of refactoring. I've managed to get the server running, and everything seems to be working properly, but I get an error when I try to add a cache system and I can't figure out if the problem is with my implementation or not.

Here is the error I get when I start the server :

TypeError: refAnnotations.errors[cacheKey].push is not a function
    at exports.ValidationError.exports.error [as annotate] (...path/server/node_modules/@hapi/validate/lib/annotate.js:53:53)

This is how my server is initialised and how the cache system is implemented :

const Hapi = require('@hapi/hapi');
const catboxMemory = require('@hapi/catbox-memory');
... rest of imports

const init = async () => {

    const server = Hapi.Server({
        host: 'localhost',
        port: config.port,
        cache:
        {
            name: 'memoryCache',
            provider: {
                constructor: catboxMemory,
                partition: 'cache'
            },
            debug: true
        },
    });

    // Register the plugins
    await server.register([
        H2o2,
        Inert,
        Vision,
        dataPlugin,
        routesPlugin
    ]);

    const start = async () => {

                ... function that handle views

        await server.start();
    };

    start();
    console.log('Server running on %s', server.info.uri);
};

process.on('unhandledRejection', (err) => {
    console.log(err);
    process.exit(1);
});

init();

I've tried several different syntaxes but the hapi documentation doesn't help me much, I can't see what I'm missing.

Thank you :)

sylvainJacot commented 1 year ago

I found the issue, I had to specify the engine of the cache module : constructor: catboxMemory.Engine

Nargonath commented 1 year ago

Glad you found the solution to your problem.