0
votes

This is the Lua script code in file test.lua:

local ips_key = 'production:ips'
local ids = redis.call('ZRANGE', ips_key, 0, '+inf', 'WITHSCORES')
local result = {}
for i, name in ipairs(ids) do
    table.insert(result, name)
end
return js.global:Array(table.unpack(result))

I'm using ioredis, which is a redis npm to eval this lua script. the return value is undefined. What am I doing wrong? Thank you!

1
Can you share with us your javascript where you use/call your .lua script? - John

1 Answers

1
votes

There are several problems with the above script.

L#2: ZRANGE does not accept +inf (or -inf) as arguments, the input should be indices in the sorted set - replace it with -1 to retrieve the entire range of elements.

L#7: There's no js library in Redis' Lua so that won't work (and isn't needed anyway)

L#7: table.unpack is Lua 5.3, whereas Redis is 5.1 - use unpack instead when needed.

L#7: No need to unpack the result you're returning (if you do, you'll just get the first element in the array back and that's it)

L#1 I understand that this is a test, but always use the KEYS input table to pass your keys to the script - never hardcode/generate key names inside it.

All in all, this should "work" when called with a single key as input:

local ips_key = KEYS[1]
local ids = redis.call('ZRANGE', ips_key, 0, -1, 'WITHSCORES')
local result = {}
for i, name in ipairs(ids) do
    table.insert(result, name)
end
return result

As a final note, the code currently "works" but does nothing - it copies the results of the call to ZRANGE and returns them. Put differently, the reply is the same as just calling ZRANGE regularly w/o Lua.