0

I want to use redis as caching for my database. I wrote the following code and it is giving me the error:

const offerData = await client.getAsync(offerID)
                               ^ TypeError: client.getAsync is not a function

My current code :

const redis = require('redis')

// Connect to the Redis server
const client = redis.createClient()

client.on('connect', () => {
    console.log('[PASS]'.green + ' Redis Connected')
    require('bluebird').promisifyAll(redis)
})

router.get('/', async (req, res) => {
 const offerData = await client.getAsync(offerID)

    let link
    if (offerData) {
        // If the offer data is in Redis, parse it and assign it to the link variable
        link = JSON.parse(offerData)
    } else {
        // If the offer data is not in Redis, get it from the database and store it in Redis
        link = await Offers.findOne({_id: offerID})
        if (link == null) return res.sendStatus(404)
        client.set(offerID, JSON.stringify(link))
    }

//Do some
})

How can I fix this? Tried that promisifyAll but no luck

2
  • I don't see getAsync in the docs. Why do you assume it exists? Commented Dec 18, 2022 at 14:07
  • @Konrad found that on a tutorial Commented Dec 18, 2022 at 14:14

1 Answer 1

1

getAsync is not a function of redis module. You can use redis-promisify module which offers this method.

Here is a sample ref for you bellow:

const redis = require('redis-promisify')

// Connect to the Redis server
const client = redis.createClient()

router.get('/', async (req, res) => {
  const offerData = await client.getAsync(offerID)
  // rest of the code
})
Sign up to request clarification or add additional context in comments.

2 Comments

Getting this error : Error Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
Make sure your Redis server is running and on port 6379, if it's on a different port you need to pass it like this const client = redis.createClient({ host: '127.0.0.1', port: 6379, db: 0 }) Last but not least, read the docs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.