Unable to properly access data tables from MongoDB - coronium:getObject( ) method

56 views
Skip to first unread message

Jonathan Sharp

unread,
Feb 7, 2016, 12:52:21 PM2/7/16
to Coronium IO
Basically, I can't access my table information properly after it returns from MongoDB via the coronium:getObject( ) method and I'm hoping there's some quick and obvious solution that has escaped me.

I started by creating an object (a table named "data") and  I'm able to access the elements of the table via the print( ) method with no problems

local data = {
name = "color",
[1] = { name = "red", value = 0},
[2] = { name = "yellow", value = 0},
[3] = { name = "blue", value = 0}
}

print( data.name )  -- outputs "color"
print( #data ) -- outputs 3
print( data[1].name ) -- outputs "red"


I then "push" the table object to MongoDB and get the objectID

mc:createObject( "soma", data, function(e) 
if not event.error then
local r = e.result.objectId 
print( r ) -- outputs "4262979782"
end
end)

       
But when I "pull" the object back to Corona and run the following code I am unable to properly access the data.

coronium:getObject( "soma", "4262979782", function(e) 
local r = e.result
print( r.name .. " from result" )  -- outputs "color"
        print( #r .. " nodes from result" ) -- *** outputs 0 
print( r[1].name .. " from result" ) -- *** Runtime error : attempt to index field '?' (a nil value) 
end)

It's probably a JSON to Lua problem, right?  Anyone have a fix for this?

Thanks,
Jonathan

Chris Byerley

unread,
Feb 7, 2016, 1:10:23 PM2/7/16
to Jonathan Sharp, Coronium IO
Hi Jonathan,

Try setting up the table array like so:

local data  = {
name = "color",
colors = { { name = "red", value = 0}, { name = "yellow", value = 0}, { name = "blue", value = 0} }
}

and then access with colors[#].name.

I also highly recommend turning on debugging on the client side if you don't have it currently active.

coronium:showStatus( true )

Hope that helps,
Cheers.


--
Learn more about the free Coronium Cloud at http://coronium.io
Free real-time Lua based game server at http://coronium.gs
Twitter: @coroniumio @coroniumgs @develephant
Site: http://develephant.net
---
You received this message because you are subscribed to the Google Groups "Coronium IO" group.
To unsubscribe from this group and stop receiving emails from it, send an email to coroniumio+...@googlegroups.com.
Visit this group at https://groups.google.com/group/coroniumio.
To view this discussion on the web visit https://groups.google.com/d/msgid/coroniumio/9c867cc4-e893-47b9-a867-e10f41938f9f%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Jonathan Sharp

unread,
Feb 8, 2016, 12:33:06 PM2/8/16
to Coronium IO, shar...@gmail.com
Thanks Chris,

It's working much better.  Sometimes I get "null" indexes in MongoDB because of the discrepancy in indexing start numbers but it seems that I can access my data much better now (like 90% accuracy).  I'm going to tinker away until I get the last 10%.  I've been loving Coronium by the way.  It's allowing me to add some really cool features to my app!

Jonathan

develephant

unread,
Feb 11, 2016, 4:20:55 PM2/11/16
to Coronium IO, shar...@gmail.com
You could also try discarding the first value of the array:

if my_arr[1] == coronium.json.null then
  table.remove( my_arr, 1 )
end

Not tested, but should work.

Cheers.

Jonathan Sharp

unread,
Feb 11, 2016, 11:13:39 PM2/11/16
to Coronium IO, shar...@gmail.com
I like that idea but wouldn't removing the [0] index (while it's still stored in JSON format) cause the [1] index to be read as  [0] and then my [2] becomes my [1] and so on?

Fortunately for my purposes, I found that as long as I get the entire object from MongoDB, Corona ignores the [0] index created by the JSON conversion and I'm back to my original object data- yay!  All of my problems occurred when I tried to getObject() JSON data from particular indexes of the MongoDB stored object and parse it in lua.  When I started loading the entire object back into Corona and then parsing the data in lua, my troubles went away.

Here's how it looks:

------- Original Table ; loaded to MongoDB via coronium:createObject() method

DNA = {
{
name = "color", 
data = 
{ name = "red", value = 0},
{ name = "yellow", value = 0},
{ name = "blue", value = 1}
}
}}





------ Coronium JSON table with null fields for index[0]

dna: [
        null, -- <= Evil null field **********
        {
            name: "color",
            data: [
                null, -- <= Evil null field **********
                {
                    name: "red",
                    value: 0
                },
                {
                    name: "yellow",
                    value: 0
                },
                {
                    name: "blue",
                    value: 1
                }
            ]
        }
     ]



-------- Corona table loaded from MongoDB JSON object ; retrieved from MongoDB with coronium:getObject() method
-------- it works perfectly (table printed with Rob Miracle's print_r module -- https://github.com/robmiracle/print_r)

[dna] => table: 0x7fe5ab6795f0 {
        [1] => table: 0x7fe5adc71c30 {
        [name] => "color"
                [data] => table: 0x7fe5ab691d10 {
              [1] => table: 0x7fe5ab672c60 {
                    [name] => "red"
                    [value] => 0
                   }
                  [2] => table: 0x7fe5ab672c60 {
                [name] => "yellow"
                  [value] => 0
                }
              [3] => table: 0x7fe5ab672c60 {
                      [name] => "blue"
                      [value] => 1

develephant

unread,
Feb 12, 2016, 3:31:52 PM2/12/16
to Coronium IO
Hi Jonathan,

Great news!

I was under the impression that you had already retrieved the data and it was in Lua (I was kind of wondering why that null was being an issue).

Coronium uses a Mongo driver to convert the results to Lua. If you circumvent that, you will get unstable (or no) results.

Additionally, if you're trying to query for specific pieces of data, without returning the whole object, there are ways to do that as well. Feel free to ask for any assistance in putting together those queries. It helps me help others in the future. :)

And finally, if you are accessing a JSON data structure from within your returned Mongo object, you will need to encode that JSON client side. Though this would be very rare as long as you utilize the server-side results.

Good luck!

Jonathan Sharp

unread,
Feb 12, 2016, 5:17:57 PM2/12/16
to Coronium IO
Thanks for your help Chris and a big thank you again for creating Coronium!  I keep getting those little "wouldn't it be cool if my app did this?"  inspirations and Coronium has made several of them possible.

Jonathan
Reply all
Reply to author
Forward
0 new messages