类型错误:客户端.getAsync不是函数- redis

vq8itlhq  于 2022-12-22  发布在  Redis
关注(0)|答案(1)|浏览(142)

我想使用redis作为我的数据库的缓存。我写了下面的代码,它给了我错误:

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

我的当前代码:

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

我该如何解决这个问题?尝试了promisifyAll,但没有成功

ltskdhd1

ltskdhd11#

  • getAsync* 不是 redis 模块的函数。您可以使用提供此方法的redis-promisify模块。

下面是一个示例参考:

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

相关问题