Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

323
Views
Node running Lua script for Redis. How to return sorted set from Lua script to Node?

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!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!