Gilles
Gilles

Reputation: 29

Don't know how to get a simple node/redis/express key with async

I am using node redis express and I want to get a simple redis key.

Unfortunately I am always stuck with "Promise <pending>".

Between "async/await, promise resolve then catch" I frankly don't understand the way it works

I would like the simplest way to get the result of my program.

app.get('*',async function(req,res) {
    res.locals.date      = Date.now();
    res.locals.appid     = req.query.appid;
    res.locals.authtoken = req.query.authtoken;
    console.log('appget',res.locals)
    exec.emit("appget",res.locals)
})

exec.on("appget",function(res) {
    console.log('appget')
    const key = 'appid:'+res.appid+'|authtoken:'+res.authtoken
    const dbx = locals.svc.db.data.db;
    var x = dbx.get(key,function(err,result) {
        console.log('func',err)
    })
    console.log('get',x)    <------  here I get   "get Promise { <pending> }
})

Thanks

I'm looking for a " simple " way to get a redis key

I found a solution :

exec.on("appget",async function(res,next) {
    console.log('appget')
    const key = 'appid:'+res.locals.appid+'|authtoken:'+res.locals.authtoken
    const dbx = locals.svc.db.data.db;
    let x = await dbx.get(key)
    if(!x) return res.status(401).end()
    console.log('result',x)
    next()
})

Upvotes: 0

Views: 21

Answers (0)

Related Questions