Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis Lua script - how to pass array as argument to a Lua script in nodejs?

Tags:

arrays

redis

lua

I am calling a Lua script from nodejs. I want to pass an array as argument. I am facing problem to parse that array in Lua.

Below is an example:

var script = 'local actorlist = ARGV[1] 
if #actorlist > 0 then
for i, k in ipairs(actorlist) do
   redis.call("ZADD","key", 1, k)
end
end';

client.eval(
     script, //lua source
     0,
     ['keyv1','keyv2']
     function(err, result) {
         console.log(err+'------------'+result);
     }
    );

It gives me this error:

"ERR Error running script (call to f_b263a24560e4252cf018189a4c46c40ce7d1b21a): @user_script:1: user_script:1: bad argument #1 to 'ipairs' (table expected, got string)

like image 373
Suyog Kale Avatar asked Nov 08 '22 03:11

Suyog Kale


1 Answers

You can do it just by using ARGV:

local actorlist = ARGV

for i, k in ipairs(actorlist) do

and pass arguments in console like this:

eval "_script_" 0 arg1 arg2 argN
like image 120
Petofi Avatar answered Nov 15 '22 05:11

Petofi