Getting variables outside from MySQL callback anonymous function?
4 replies, posted
Well, I have very basic problem with getting variables out from callback function.
[CODE]
local data = false
print("----derived vars")
MySQLite.query("some query",
function (resp)
if resp then
print("----got resp")
data = resp
end
end,
function (error)
print("error msg")
end
)
print(data)
return data
[/CODE]
So the query is executed successfully because I'm getting "----got resp" message, but "data" variable keeps being "false". I expect that this is a common issue but honestly I do not know how to make a search phrase for that :D
It's simple, data is false because the callback function is not executed yet when you print its value. Once the callback gets executed, data will be set to the actual response. There's no way to retrieve this value any earlier than that.
[QUOTE]It's simple, data is false because the callback function is not executed yet when you print its value. Once the callback gets executed, data will be set to the actual response. There's no way to retrieve this value any earlier than that.[/QUOTE]
Thanks for reply. Is there a way to get needed data from query inside the function? Because code from OP post is used in function which only requests data from database so it must return result in some way.
No, not possible. You must handle it in the callback. You can't return something that doesn't exist yet.
[QUOTE]No, not possible. You must handle it in the callback. You can't return something that doesn't exist yet.[/QUOTE]
Thanks, I'll try to do it that way.
Sorry, you need to Log In to post a reply to this thread.