1
votes

I am using Lua script for reading contents of a hashkey in redis store. My code is below:

local key = KEYS[1]

-- Check if the user account exists
local accnt = redis.call('hgetall', key);
if next(accnt) == nil then return 404 end

return accnt;

When I run this program, I get the results correctly as below.

[2016-04-17 19:27:07.807] [DEBUG] AuthServer - Loading script ./scripts/debit_script.lua to redis...
[ 'id',
  '47',
  'accType',
  '1',
  'creditLimit',
  '75000',
  'creditConsumed',
  'null' ]

But when I try to return a single value from the above lua table (say accnt['id'], or accnt.id I get null. Here is the new version of program that accesses a single entry in table by key, but fails.

local key = KEYS[1]

-- Check if the user account exists
local accnt = redis.call('hgetall', key);
if next(accnt) == nil then return 404 end

return accnt['id'];

I heard that lua table is an associative array of key:value pairs. So, my code for accessing an entry in the table seems to be correct. Isn't it?

What am I doing wrong here, and how do I access a single key correctly?

** EDIT ** In order to find the type of "accnt", I modified my code to return as

return type(accnt)

the result is as below:

[2016-04-17 20:42:02.229] [DEBUG] AuthServer - Loading script ./scripts/debit_script.lua to redis...
table
debit result: table

So, I am wondering if lua returns a regular array or a table, when we query redis hash. Thankx for anyone pointing me at a right solution.

1
The problem is that accnt is actually a regular array.vaultah
I tried to return the type of accnt variable. It returned as table.Mopparthy Ravindranath

1 Answers

0
votes

OK. Its a table with keys as numbers 1..n and values as what was returned by Redis. I was thinking that my redis npm would convert the return data into a proper table mimicking the hash of redis. I will have to do it myself.