Lua 函数返回空值

我有一个问题,关于一个返回表格的函数。这个表格从 DB 行中获取数据(在另一个函数),然后将其发送给客户端显示。我初始化了表格并填充了随机数据,正确返回了数据。函数正确地打印了 getSData 中的所有项,但是当返回数据时,它没有返回任何内容,甚至在最后的 print 函数中都将整个表格转储。

function getBourse()
    local result = {
        {
            libelle = "随机物品名称",
            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("物品名称 "..tostring(g).." 数量 "..tostring(i.out_money))
                        table.insert(result,{libelle = tostring(g), price = tostring(i.out_money), difference = 0.0})
                    end
                end
            end
        end
        print("测试 ", 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 部分获取所有内容的速度过慢,而函数已经到达了 return。希望我能很好地解释我要做的事情,因为英语不是我的母语。

我的问题解决方法如下:

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
点赞