Reputation: 33
I'm trying to get/set in redis using nodejs, I've ensured its async/await as per the docs but from my code below im only ever hitting console log 1.
Am I missing something obvious?
Thanks
const Redis = require('redis')
const redisClient = Redis.createClient({
url: 'redis://*****:*****@redis-******.cloud.redislabs.com:16564'
})
redisClient.connect()
redisClient.on('error', (err) => console.log('Redis Client Error', err));
redisClient.set('name', 'Steph'); // THIS WORKS FINE
router.get('/market/product/:slug', async (req,res) => {
const productId = req.params.slug
console.log('console log one')
await redisClient.get(productId, async (error, history) => {
console.log('console log two')
if (error) console.error(error)
if (productId != null) {
console.log('console log three')
res.json(JSON.parse(history))
} else {
console.log('console log four')
await ProductPrices.find({productId: productId})
.lean()
.sort({date: 1})
.exec((err, data) => {
if (err) {
return res.status(400).json({
error: errorHandler(err)
})
}
redisClient.set(productId, JSON.stringify(data))
res.json(data)
})
}
})
})
Upvotes: 0
Views: 408
Reputation: 150
I went to the documentation of the package you stated you used in the comments of the question. I couldn't find any place where callbacks were used.
Try
let history= await redisClient.get(productId)
if (!history) {
if (productId != null) {
console.log('console log three')
res.json(JSON.parse(history))
} else {
console.log('console log four')
await ProductPrices.find({ productId: productId })
.lean()
.sort({ date: 1 })
.exec((err, data) => {
if (err) {
return res.status(400).json({
error: errorHandler(err)
})
}
redisClient.set(productId, JSON.stringify(data))
res.json(data)
})
}
}
Upvotes: 1