fastify / fastify-redis

Plugin to share a common Redis connection across Fastify.
MIT License
198 stars 31 forks source link

Unable to use methods of the plugin after registering #167

Closed Zireael26 closed 1 year ago

Zireael26 commented 1 year ago

Prerequisites

Fastify version

^4.0.0

Plugin version

^6.1.0

Node.js version

18

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

12.6.1

Description

After registering the plugin with fastify like so:

src/plugins/redis.ts

import fp from 'fastify-plugin'
import fastifyRedis, { FastifyRedisPluginOptions } from '@fastify/redis'

/**
 * This plugins enables redis support
 *
 * @see https://github.com/fastify/fastify-redis
 */

export default fp<FastifyRedisPluginOptions>(async (fastify) => {
    fastify.register(fastifyRedis, {
        host: 'localhost',
        port: 6379,
        db: 0,
        autoResendUnfulfilledCommands: true,
        closeClient: true,
        // enableReadyCheck: true,
        // autoResubscribe: true,
    });
})

When I try to use it in my file src/services/RedisService.ts like so:

import fastify from 'fastify';

const getFromRedis: Function = async (key: string) => {
    const { redis } = fastify();
    const data = await redis.get(key);
    return data;
}

const setToRedis: Function = async (key: string, value: string) => {
    const { redis } = fastify();
    await redis.set(key, value);
}

export const deleteFromRedis: Function = async (key: string) => {
    const { redis } = fastify();
    await redis.del(key);
}

const existsInRedis: Function = async (key: string) => {
    const { redis } = fastify();
    const exists = await redis.exists(key);
    return exists;
}

gives me the error

err: {
[App]       "type": "TypeError",
[App]       "message": "Cannot read properties of undefined (reading 'get')",
[App]       "stack":
[App]           TypeError: Cannot read properties of undefined (reading 'get')
[App]               at getFromRedis (/Users/abhishek.ka/PersonalProjects/atlas-backend/dist/services/RedisService.js:10:30)
[App]               at getHelloFromRedis (/Users/abhishek.ka/PersonalProjects/atlas-backend/dist/services/RedisService.js:84:25)
[App]               at getHello (/Users/abhishek.ka/PersonalProjects/atlas-backend/dist/services/GeneralInfoService.js:26:55)
[App]               at Object.<anonymous> (/Users/abhishek.ka/PersonalProjects/atlas-backend/dist/routes/root.js:21:64)
[App]               at preHandlerCallback (/Users/abhishek.ka/PersonalProjects/atlas-backend/node_modules/fastify/lib/handleRequest.js:128:37)
[App]               at preValidationCallback (/Users/abhishek.ka/PersonalProjects/atlas-backend/node_modules/fastify/lib/handleRequest.js:112:5)
[App]               at handler (/Users/abhishek.ka/PersonalProjects/atlas-backend/node_modules/fastify/lib/handleRequest.js:76:7)
[App]               at handleRequest (/Users/abhishek.ka/PersonalProjects/atlas-backend/node_modules/fastify/lib/handleRequest.js:24:5)
[App]               at runPreParsing (/Users/abhishek.ka/PersonalProjects/atlas-backend/node_modules/fastify/lib/route.js:528:5)
[App]               at next (/Users/abhishek.ka/PersonalProjects/atlas-backend/node_modules/fastify/lib/hooks.js:168:7)
[App]     }

I have been trying for a while now, and have tried multiple things.

Steps to Reproduce

  1. Register plugin
  2. Get fastify instance
  3. Call any of get/set/del/exists

Expected Behavior

The redis function is called and appropriate action is executed.

mcollina commented 1 year ago

This would do:

import fp from 'fastify-plugin'
import fastifyRedis, { FastifyRedisPluginOptions } from '@fastify/redis'

/**
 * This plugins enables redis support
 *
 * @see https://github.com/fastify/fastify-redis
 */

export default fp<FastifyRedisPluginOptions>(async (fastify) => {
    await fastify.register(fastifyRedis, {
        host: 'localhost',
        port: 6379,
        db: 0,
        autoResendUnfulfilledCommands: true,
        closeClient: true,
        // enableReadyCheck: true,
        // autoResubscribe: true,
    });
})