I have a problem with a function that returns a table. That table is filled with data from a DB row (on another function) and then the function send that to the client to be displayed. I initialized the table and filled with random data, which is correctly returned. The function correctly prints all the items inside getSData, but when it's time to return it doesn't return anything, and even dumps the whole table in the last print function.
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
this is how getSData works:
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
The problem I'm experiencing is like the getSData part is slow to fetch everything and the function already got to the return. Hope I explained what I'm trying to do well, since English isn't my main language.
The solution to my problem is this one:
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