Logs --
16:12:35 Lua Error: [ERROR] addons/tttdamagelogs-master/lua/rdm_manager/sv_rdm_manager.lua:472: attempt to index field 'database' (a nil value)
Code which starts at line 471 --
local encoded = util.TableToJSON(tbl)
local update = Damagelog.database:query("UPDATE damagelog_previousreports SET report = "..sql.SQLStr(encoded).." WHERE _index = "..tbl.index..";")
update:start()
I am wondering why I am receiving the error as stated above, referencing to line 472? Thanks
Because Damagelog does not contain a key named database. Most likely cause is a typo, for example perhaps it should be damagelog or DamageLog (Lua names are case sensitive).
Related
local file = 'data.txt'
local lines = lines_from(file)
global data = {} -- this line is giving the error "lua: day8.lua:20: '=' expected near 'data'"
for key, value in pairs(lines) do
local row = {}
for i=1, #value do
local char = value:sub(i,i)
row[i] = char
end
data[key] = row
end
As you can see from the code above, my code is throwing an error on the line where the variable data is initialised. This code was working earlier when I tested it, then I added more code below what is visible and it broke this line somehow.
I don't think its the code below that broke this line as otherwise why would it be showing there?
This is also my first ever code with lua so I have no experience with this language.
What could be wrong in this code that could cause this error
Global variables don't need to be declared explicitly like local ones do. The interpreter is erroring because you are prefixing global to your variable, which isn't a recognized keyword
Try the following, without the global:
data = {}
I have a lua script with code block as below:
local call_data = cjson.decode(ARGV[1])
local other_data = cjson.decode(ARGV[2])
local data = {}
local next = next
local populate_data = function(source)
if next(source) == nil then
return
end
for property,value in pairs(source) do
redis.call('HSET', KEYS[2], property, value)
end
end
populate_data(call_data)
populate_data(other_data)
When I try to run the script with the following command KEYS and ARGV as:-
redis-cli --eval dialed.lua "inflight_stats:18" "calls:AC443d7a8111a96ba8074f54a71f0521ce:CA1ec49703eee1959471c71506f43bb42e:dialed" , "{\"from\":\"+18035224181\",\"to\":\"+919943413333\",\"sid\":\"CA1ec49703eee1959471c71506f43bb42e\",\"status\":\"queued\",\"direction\":\"outbound-api\",\"date_created\":null,\"account_sid\":\"AC443d8a8111a96ba8074f54a71f0521ce\"}" "{\"phone\":\"919943413333\",\"campaign_id\":18,\"caller_session_sid\":\"CA828b163153bf5cc301ef5285e38925f9\"}" 0
Error :-
(error) ERR Error running script (call to f_08dcc69ee8baa0200e0cf552948ab4bc338c9978): #user_script:11: #user_script: 11: Lua redis() command arguments must be strings or integers
TL;DR for values returned by cjson.decode(), use cjson.null to compare to JSON's null value.
Explanation: Lua uses nil in tables to mark deleted entries. If JSONinc nulls were converted to Lunatic nils, the decoded objects would be corrupt. Therefore, the cjson lib uses a lightweight userdata type to represent null/nil.
Your 'call_data' has a 'date_created' field that is null - that causes the error.
The funny thing is that Redis, like Lua, will not store a nil/null value, so you'll have to either ignore null values or use a special value in Redis to flag them.
Assuming you'll be ignoring them, here's one way around it:
local call_data = cjson.decode(ARGV[1])
local other_data = cjson.decode(ARGV[2])
local data = {}
local next = next
local null = cjson.null
local populate_data = function(source)
if next(source) == nil then
return
end
for property,value in pairs(source) do
if value ~= null then
redis.call('HSET', KEYS[2], property, value)
end
end
end
populate_data(call_data)
populate_data(other_data)
Also, a small optimization would be to batch the updates, like so:
local payload = {}
for property,value in pairs(source) do
if value ~= null then
table.insert(payload, property)
table.insert(payload, value)
end
end
redis.call('HSET', KEYS[2], unpack(payload))
P.S. if you want, look at ReJSON that I wrote - it is designed to help with what it appears that you're trying to do.
I'm using this (old) Lua module to save and access tables from local files (if there is a better way, please shout I'm not invested) -- https://github.com/gideros/table.save/blob/master/table.save-0.94.lua
It's been working great, but now I'm trying to save key value pairs I'm getting the error:
attempt to call global exportstring (a nil value)
I'm new to Lua. The error is on line 108 on that file I linked to. Any ideas?
I've tried creating blank tables, or basic tables without key value pairs, this works fine. It's when I come to insert strings the issues arise.
This will work:
local myTable = {}
myTable[0] = 2
inputOutputTable.save(myTable, "testytesty")
local testy = inputOutputTable.load("testytesty")
print("Testing 123... " .. testy[0])
However this wont:
local myTable = {}
myTable["welcome"] = "1"
I get : attempt to call global 'exportstring' (a nil value)
Expected results - it saves the table.
What happens - it throws the error.
So I have a table using Lua and within it is a variable, but I can't figure out how to reach it because of its name. I was able to use json.prettify( _id ) in order to view whats in the table. All I want is the id within this table but because of how it's named I can't seem to get what's inside of _id, I tried both
_id[1] but that == nil
_id.$oid but that gave me a runtime error
printed response: _id = {
"$oid":"597015b757203b04d6941d45"
}
interesting note is that #_id == 0
Use
tableName[ 'variableName' ]
or
tableName.variableName
Note: Sometimes you can not use both form for example:
tableName[ 'name with spaces' ] -- ok
tableNames.name with spaces -- error
I think operator # can not be used with non-indexed tables.
You need decode json string to Lua table and get value in any way:
local json = require("json")
local str = [[ { "$oid":"597015b757203b04d6941d45" } ]]
local t_res= json.decode(str)
-- access
print( t_res["$oid"] )
-- or
local k,v = next(t_res)
print( v )
-- or
for k, v in pairs(t_res) do
print(v)
break
end
PS: Lua operator # only for a regular array, with non-nil values.
In order to overcome the fact that the name of the variable included the '$' symbol the correct way to get the variable within the table was to use:
_id['$oid'], this worked.
_id.$oid gave a runtime error.
I have imported to my MediaWiki site the it.Wikipedia Modulo:Bio but I get this error:
Error Lua in Module:Bio line 700: attempt to index field 'wikibase' (a nil value).
In line 700 I have this code:
local entity = mw.wikibase.getEntityObject()
I have multiple wikis that shares the same source code, and the same database, but with its own tables. Then my wikibase is mybase.mywiki.com.
I tried to solve by changing wikibase to mybase:
local entity = mw.mybase.getEntityObject()
But it doesn't work.
The problem is not wikibase: the error simply says that there is no field named wikibase in the mw table, so the problem is that mw doesn't contain what you think it should. You must find the code that puts wikibase field in mw. If it does something like mw.wikibase = something and something is nil, then it is as if that line had not executed (it is not an error to assign nil to a table field, it is like removing the field if it exists already, and doing nothing if it doesn't exist). This is common error when something is an function call, the function may return nil under some circumstances.