lua redis 字符串比较不工作

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 

密码=admin redis密码=admin

我能够看到两个密码都作为输出 admin 但它在 lua 代码中不匹配并且控制总是转到其他部分。

当我这样比较的时候

if (tostring(密码) == "admin" )

它工作正常,这意味着问题出在 redis 值上,但我已经在 redis 中设置了密码值 admin。

我的理解是 hmget returns multi-bulk reply,结果是 lua table,所以你应该这样做:

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

来自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.

请注意,ngx.null 值与 Lua nil 值不同,因此您可能需要单独检查。

另请注意,您在两个不同的地方使用 None 作为变量并使用 "None" 作为字符串,这可能不会达到您的预期。