7

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)

5
  • What is the value of ARGV[1] suppose to be? How does it get its value? eg. is it a string containing comma separated list of actors? Commented Jan 9, 2017 at 13:59
  • According to redis.io/commands/eval, you should be using ARGV or KEYS called with client.eval(script, 2, ... Commented Jan 9, 2017 at 14:07
  • @greatwolf, ARGV[1] would be array of values like ['keyv1','keyv2'] Commented Jan 10, 2017 at 10:32
  • Not according to the error message. It sees a string, not an array. Commented Jan 10, 2017 at 11:40
  • Judging from this issue, looks like js arrays turn into strings when passed into eval. Is there a reason for not unpacking the array first and passing them in as separate arguments? Commented Jan 10, 2017 at 12:02

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

Comments

-1

You can only pass in strings into Redis lua script.

If you need an array of values to be pass into Redis lua script, you can do this:

let script = `
if #ARVG > 0 then
    for i, k in ipairs(ARGV) do
       redis.call("ZADD","key", 1, k)
    end
end`;

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

The key here is to pass keyv1 and keyv2 as a separate params when calling eval. (I am using es6 syntax here)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.