我正在从nodejs 调用Lua 脚本。我想传递一个数组作为参数。我在 Lua 中解析该数组时遇到问题。
下面是一个例子:
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);
}
);
Run Code Online (Sandbox Code Playgroud)
它给了我这个错误:
“ERR 运行脚本时出错(调用 f_b263a24560e4252cf018189a4c46c40ce7d1b21a):@user_script:1:user_script:1:'ipairs' 的错误参数 #1(预期表,得到字符串)
您只需使用ARGV即可做到这一点:
local actorlist = ARGV
for i, k in ipairs(actorlist) do
Run Code Online (Sandbox Code Playgroud)
并在控制台中传递参数,如下所示:
eval "_script_" 0 arg1 arg2 argN
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7274 次 |
| 最近记录: |