user3342210
user3342210

Reputation: 23

lua function not returning data

I am having trouble with a Lua function. I can set the return value for sendAction to a string ("test") and it will return properly. However I can't get the variable of "data" to return as it always returns nil. What am i doing wrong?

local json = require("json");
local action = {};
local action_mt = { __index = action }

---------PRIVATE FUNCTIONS------------

function action:sendAction(values, networkListener)
    local data,pos,msg = "";
    local params = {}; params.body = "";
    for key,value in pairs(values) do
        params.body = params.body .. "&" .. key .."=" .. value
    end

    local function networkListener( event )
        if ( event.isError ) then
            print( "Network error!" );
        else
            data,pos,msg = json.decode( event.response );
            if (data.errors.count > 0) then
                print("errors");
            end
        end
        return data;
    end

    network.request( "http://127.0.0.1/action.php", "POST", networkListener, params )
end

------PUBLIC FUNCTIONS------
function action:new(action) 
    local newAction = { action = action };
    return setmetatable( newAction, action_mt )
end

function action:createSession()
    local data = action:sendAction( { action = "createSession" } );
    print(data);
end

return action;

Upvotes: 1

Views: 776

Answers (1)

Mud
Mud

Reputation: 29021

sendAction contains no return statement (in its scope). Why would you expect it to return anything?

The call to network.request is asynchronous, meaning the request actually happens in a separate thread of execution that runs parallel to your main code execution, so the request to the server and response from the server will happen after sendAction has returned.

You want to use the same model network.request does. That is, you pass a callback to sendAction which receives results when they become available. That's a very typical pattern for asynchronous code.

function action:sendAction(values, onSendActionComplete)
    local params = {}; params.body = "";
    for key,value in pairs(values) do
        params.body = params.body .. "&" .. key .."=" .. value
    end

    local function networkListener( event )
        if event.isError then
            onSendActionComplete(false, "Network error.");
        else
            local data,pos,msg = json.decode( event.response );
            if data.errors.count > 0 then
                onSendActionComplete(false, "JSON decode error.");
            else
                onSendActionComplete(true, data);
            end
        end
    end

    network.request( "http://127.0.0.1/action.php", "POST", networkListener, params )
end

------PUBLIC FUNCTIONS------
function action:new(action) 
    local newAction = { action = action };
    return setmetatable( newAction, action_mt )
end

function action:createSession()
    local function onSendActionComplete(success, data)
        if success then
            print(data);
        else
            print('Error:', data)
        end
    end
    action:sendAction( { action = "createSession" }, onSendActionComplete)
end

Upvotes: 3

Related Questions