lua按键访问表元素

时间:2016-04-17 15:05:19

标签: lua redis

我使用Lua脚本在redis商店中读取hashkey的内容。我的代码如下:

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;

当我运行此程序时,我会正确地得到如下结果。

[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' ]

但是当我尝试从上面的lua表中返回一个值时(比如accnt [' id']或accnt.id我得到null。这是访问单个的程序的新版本按键输入,但失败。

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'];

我听说lua表是key:value对的关联数组。所以,我访问表中条目的代码似乎是正确的。不是吗?

我在这里做错了什么,以及如何正确访问单个密钥?

**编辑** 为了找到" accnt"的类型,我修改了我的代码以返回为

return type(accnt)

结果如下:

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

所以,当我们查询redis hash时,我想知道lua是返回常规数组还是表。感谢任何指点我正确解决方案的人。

1 个答案:

答案 0 :(得分:0)

行。它是一个表格,其中键为数字1..n,值为Redis返回的值。我在想我的redis npm会将返回数据转换成一个模仿redis散列的正确表格。我必须自己做。

相关问题