当前位置: 首页 > 面试题库 >

Catbox-redis在我的hapijs应用程序上显示断开连接的错误

马弘益
2023-03-14
问题内容

我曾经使用过catbox-redis插件,但是当我运行代码时,出现了断开连接错误。当我搜索出现此错误的位置时,发现它来自catbox lib
client.js isReady函数

如果有任何关于此的请帮助我

    {
    method : 'POST',
    path   : "/signup",
    config : {
        tags        : ['api'],
        description : 'Customer signup',
        validate    : {
            failAction: Relish.failAction,
            options   : {
                abortEarly: false,
            },
            payload   : signupSchema,
        }
    },
    handler: function(request, response){
        let responseData = {
            'message': 'Data inserted', 
            'errors': [],
            'data': [
                {
                    'id': 'name'
                }
            ]
        };

        const options = {
            //partition: 'examples',               // For redis this will store items under keys that start with examples:
            host: '127.0.0.1',                   // If you don't supply, 127.0.0.1 is the default
            port: 6379,                        // If you don't supply, 6379 is the default
            password: ''                         // If you don't supply, auth command not sent to redis
        };

        var client = new Catbox.Client(require('catbox-redis'), options);    // Chance require('../') to 'catbox-redis' when running in your project
        client.start((error) => {
            console.log('Cache server started');
            console.log('---------------------------------');
        });

        const key = {
            segment: 'example',
            id: 'myExample'
        };

        const cacheValue = 'my example';
        const ttl = 10000;                         // How long item will be cached in milliseconds

        client.get(key, (err, cached) => {

            if (err) {
                console.log(err);
            }
            else if (cached) {
                return callback(null, 'From cache: ' + cached.item);
            }

            client.set(key, cacheValue, ttl, (error) => {

                if(error)
                    console.log(error);
                console.log("Cache was set on the redis");
                console.log('---------------------------------');
            });
        });


        return response(responseData);
    }
}

问题答案:

终于我发现了。因为当我的Redis启动过程进行时,异步会尝试获取我的Redis密钥。现在我将其放入redis的回调中,将函数放入redis中,开始一切正常



 类似资料: