lua - Lua 函数返回 Nil
问题描述
我对返回表的函数有疑问。该表填充了来自数据库行(在另一个函数上)的数据,然后函数将其发送到客户端以进行显示。我初始化了表格并填充了正确返回的随机数据。该函数正确地打印了 getSData 中的所有项目,但是当它需要返回时,它不会返回任何内容,甚至将整个表转储到最后一个打印函数中。
function getBourse()
local result = {
{
libelle = "Any random item name",
price = 830728,
difference = 0.0
}
}
vRP.getSData({"vRP:economy_trs", function(data)
local economy_trs = json.decode(data) or {}
for k,v in pairs(economy_trs) do
local htr = economy_trs[k]
for g,i in pairs(htr) do
if i ~= nil or g ~= nil then
if g ~= "timestamp" then
print("itemname "..tostring(g).." amount "..tostring(i.out_money))
table.insert(result,{libelle = tostring(g), price = tostring(i.out_money), difference = 0.0})
end
end
end
end
print("test ", dump(result))
end})
return result
end
这就是 getSData 的工作原理:
function vRP.getSData(key, cbr)
local task = Task(cbr,{""})
MySQL.query("vRP/get_srvdata", {key = key}, function(rows, affected)
if #rows > 0 then
task({rows[1].dvalue})
else
task()
end
end)
end
我遇到的问题就像 getSData 部分获取所有内容的速度很慢,并且函数已经返回。希望我解释了我正在努力做好的事情,因为英语不是我的主要语言。
我的问题的解决方案是这个:
function getBourse(cbr)
local task = Task(cbr,{""})
local result = {}
vRP.getSData({"vRP:economy_trs", function(data)
local economy_trs = json.decode(data) or {}
for k,v in pairs(economy_trs) do
local htr = economy_trs[k]
for g,i in pairs(htr) do
if i ~= nil or g ~= nil then
if g ~= "timestamp" then
table.insert(result,{libelle = tostring(g), price = tonumber(i.out_money), difference = 0.0})
end
end
end
end
task({result})
end})
end
解决方案
推荐阅读
- r - 逻辑 glmer 的 sjplot::plot_model 中轴限制和比例的特定操作,通常保留 plot_model 输出
- laravel - 调用另一个控制器时 Laravel 错误
- java - 如何以编程方式在 TextInputLayout 中设置 endIconMode
- javascript - 当事件作为参数在jquery中的该函数中传递时,如何在'keydown'上调用函数
- graphql - 生成 Prisma Graphql Schema 时使用生成和部署后挂钩之间的区别?
- c# - 如何让子游戏对象影响父对象?
- javascript - 如何从限制线chart.js中分离第一个和最后一个数据点
- sql - 如何连接两个具有相同值的不匹配表?甲骨文 SQL
- javascript - Javascript Google Chrome 控制台、日志和功能保存
- python - 如何检查视频文件是否使用 discord.py 发送到服务器?