2

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
Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64
pcolombo2
  • 21
  • 2
  • 1
    So, is `setSData` a kind of an _asynchronous_ function? What does it return? Does it have a callback parameter? – 9000 Oct 11 '19 at 16:16
  • I edited my question with how getSData works. It has a callback parameter – pcolombo2 Oct 11 '19 at 16:34
  • 1
    So your function returns without waiting for the async function to complete, and the real data is passed to the callback. I don't know if there is a way to _wait_ for a coroutine to complete inside a function; that's what you would need. I could not find it. If there's none, you'll have to make the rest of your design async / coroutine-based. – 9000 Oct 11 '19 at 16:41
  • Thank you, added a callback to the function and is now working just fine! – pcolombo2 Oct 11 '19 at 17:00

0 Answers0