Sending data pulled from SQL back to client

Hello all,

I have issues sending data back to the client. I believe it is because I am not capable of converting the table value into a string, but can’t find out how to do it so any help would be appeciated. I can get the values to be show on server console

[{“race_name”:“bleit”},{“race_name”:“bweurk”},{“race_name”:“laatstetestvanavond”},{“race_name”:“square”}]

RegisterNetEvent("StreetRaces:listRaces_sv")
AddEventHandler("StreetRaces:listRaces_sv", function()
local playerRaces = {}
MySQL.ready(function()
    MySQL.Async.fetchAll("SELECT race_name FROM race",{}, function(result)
    playerRaces = print(json.encode(result))

    
    notifyPlayer(source, playerRaces)
    end)
end)

end)

Error I receive in server console InvokeNative: execution failed: Argument at index 1 was null.
SCRIPT ERROR: Execution of native 000000002f7a49e6 in script host failed.

What is notifyPlayer?

-- Helper function for notifying players

function notifyPlayer(source, msg)

    -- Add custom notification here (use chat by default)

    TriggerClientEvent('chatMessage', source, "[StreetRaces]", {255, 0, 0}, msg)

end

Try the following

RegisterNetEvent("StreetRaces:listRaces_sv")
AddEventHandler("StreetRaces:listRaces_sv", function()
local playerRaces = {}
local player = source
MySQL.ready(function()
    MySQL.Async.fetchAll("SELECT race_name FROM race",{}, function(result)
    playerRaces = print(json.encode(result))

    
    notifyPlayer(player, playerRaces)
    end)
end)

end)

That got rid of the error message in the console! Thank you!
It also sent the notify to the player, however no data is displayed apart from from the standard “StreetRaces” defined in the notifyplayer function.
This is because the result needs to be put in a string?

Edit: also why does it work if you put the source in player but not if you use source itself for the destination? Sorry for the beginner questions but am trying to understand :-/

Its a table, You need to dump it in order to print it. Or Json.decode, to put it into a lua table format, then dump it properly.

Remove print when you assign it to the playerRaces variable.

As for the player variable, it is to do with function and variable scope. Try printing the player variable and source to the console within the mysql callback function to see the difference of output (might help you understand what’s happening)

1 Like

Pf ok it’s getting difficult for me and to me it seems that the json decode is not the easiest way to go. I had some succes yesterday by doing this code. By success, I mean when I replaced the msg = msg … part by print(result[counter].race_name I had a nice one below the other in the console that seemed more “ok” to export to a client.

- ML start list recorded races
RegisterNetEvent("StreetRaces:listRaces_sv")
AddEventHandler("StreetRaces:listRaces_sv", function()
local playerRaces = {}
local player = source

local counter = 1
local msg = "Races: "
    MySQL.ready(function()
    MySQL.Async.fetchAll("SELECT race_name FROM race",{}, 
        function(result)
        while result[counter].race_name ~= nil do
        msg = msg .. " " .. tostring(result[counter].race_name)
        counter = counter + 1
        end
    notifyPlayer(player, msg)
    end)
end)

end)

edit: ignore this post for now

Ok that looks better already. Now the result is getting to the client indeed in table form!

-- ML start list recorded races
RegisterNetEvent("StreetRaces:listRaces_sv")
AddEventHandler("StreetRaces:listRaces_sv", function()

local player = source

local counter = 1
local msg = "Races:"
    MySQL.ready(function()
    MySQL.Async.fetchAll("SELECT race_name FROM race",{},
        function(result)       
        
            while result[counter].race_name ~= nil do
            local check = result[counter].race_name
            notifyPlayer(player, "Race:" .. check)
            counter = counter + 1
            end
       end)
    end)
end)

So above code works and I can see a list of all races on the client. Unfortunately, I was not able to work out how to send it in 1 string to the client.

I know it is asking a lot, but is anyone willing to just show me how to concat the values I receive from the SQL and put it in a single string?

RegisterNetEvent("StreetRaces:listRaces_sv")
AddEventHandler("StreetRaces:listRaces_sv", function()
local player = source
local msg = ""
	MySQL.ready(function()
	MySQL.Async.fetchAll("SELECT race_name FROM race",{},
		function(result)     
			for race = 1, #result do
				msg = msg .. "Race: " .. result[race].race_name .. " | "
			end
			notifyPlayer(player, msg)
		end)
	end)
end)

This will concat the races into one string.

1 Like

Thank you kind sir, works like a charm.