as1992
as1992

Reputation: 53

lua redis string comparison not working

local password = json_string["password"] or "None"
local redisPassword = red:hmget(userName,"password") or None
local redisAuthtoken = red:hmget(userName,"authToken")
if (tostring(password)   ==  tostring(redisPassword))
then
    ngx.say(redisAuthtoken) 
else
    ngx.say("Error User or Service Not found 1510")
end 

password = admin redisPassword = admin

i am able to see both password as output admin but it is not matching in lua code and control always going to else part.

when i am comparing like this

if (tostring(password) == "admin" )

it is working fine which means the issue is with the redis value but i have set password value admin in redis.

Upvotes: 1

Views: 999

Answers (1)

Paul Kulchenko
Paul Kulchenko

Reputation: 26744

My understanding is that hmget returns multi-bulk reply, which has the result in a lua table, so you should probably do something like this:

local res, err = red:hmget(userName,"password")
local redisPassword
if res and res ~= ngx.null then redisPassword = res[1] end
ngx.say(redisPassword)

From the documentation:

A non-nil Redis "multi-bulk reply" results in a Lua table holding all the composing values (if any). If any of the composing value is a valid redis error value, then it will be a two element table {false, err}. A nil multi-bulk reply returns in a ngx.null value.

Note that ngx.null value is not the same as Lua nil value, so you may need to check for that separately.

Also note that you use None as a variable and "None" as a string in two different places, which may not do what you expect.

Upvotes: 2

Related Questions