Anon
Anon

Reputation: 396

TypeError: client.getAsync is not a function - redis

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

Upvotes: 0

Views: 2076

Answers (1)

Moinul Haq
Moinul Haq

Reputation: 123

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

Upvotes: 1

Related Questions