首页 > 解决方案 > 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

标签: luagrand-theft-auto

解决方案


推荐阅读