Storage_write(obj) vs pcall(storage_write, {obj}

Hi,

I’m new to Nakama, and I’m really impressed with it so far.

I’m having a problem with a server side RPC written in Lua.
I try to write some data to the object storage, it works fine if I use a pcall(nk.storage_write, { obj }), but if I try nk.storage_write(obj) as it shows in the docs the object doesn’t get written to the database.

The same happens when trying to read with storage_read. I’ve included some code snippets below.

    local object = {
        collection = "player",
        key = "data",
        user_id = user_id
    }  
    local objects = nk.storage_read(object)

    local object = {
        collection = "player",
        key = "data",
        user_id = user_id
    }
    local success, objects = pcall(nk.storage_read, { object })

The pcall returns the object correctly, the nk.storage_read() returns nothing.

What am I doing wrong?

Thanks.

Hello @Omee,

Both functions, storage_write and storage_read, were written to accept lua tables so I believe it’s an inconsistency in the documentation. Could share the links to the snippets you based your code on?

Under the Storage Engine section. Change the code examples to Lua and you can see a read and write using nakama.storage_read and nakama.storage_write.

Ok, now that I’ve looked at the examples more closely, I can see that the objects should be nested in another Lua table like so:

local object = {
        collection = "player",
        key = "data",
        user_id = user_id
    }  
    local objects = nk.storage_read( { object } )

That code works. I’m new to Lua, so I’m not sure if that is something that is obvious in the documentation or if it could be a bit clearer? The examples in the documentation all involve reading and writing passing in multiple objects as the parameter. Maybe an example passing in a single object would be beneficial?

Thanks for the quick response!