Error on implementing cache system with @hapi 21.3.2

121 views Asked by At

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.

My environment :

Node : 16.20.2
@hapi/hapi : 21.3.2
@hapi/catbox : 12.1.1
@hapi/catbox-memory : 6.0.1

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
            }
        },
    });

    // 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.

1

There are 1 answers

0
Sylvain Jacot On

I found by chance the answer to my question in changelog of hapi. I had to call the Engine of catBoxMemory. Here is the fixe :

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