Storage write rejected - version check failed

Hi there,
I am trying to set up data on the Nakama runtime start up by following this tutorial, but I am getting the error

Storage write rejected - version check failed

when calling

nk.storageWrite(<writeObjects>)

My code looks as such, where TREASURES is a list of my Treasure class defined elsewhere:

const writeObjects: nkruntime.StorageWriteRequest[] =
        TREASURES
            .map(treasure => {
                // create a write request object for each Treasure
                const writeRequest: nkruntime.StorageWriteRequest = {
                    userId: "00000000-0000-0000-0000-000000000000",
                    collection: TREASURES_COLLECTION_NAME,
                    key: `${treasure.treasureId}`,
                    permissionRead: TREASURES_PERMISSION_READ,
                    permissionWrite: TREASURES_PERMISSION_WRITE,
                    value: treasure,
                };
                return writeRequest;
            });
try {
        nk.storageWrite([...writeObjects]);
    } catch (error) {
        logger.error('Treasures storageWrite error: %q', error);
        throw error;
    }

I saw a previous post where it was suggested to take out the version attribute from the nkruntime.StorageWriteRequest, but as you can see from my snippet, I do not have the optional version attribute on my request objects :cry:

I would like to be able to do version: "*", as it says in the documentation that it will only create a storage object if it does not exist in the database - which is what I truly want.

How can I

  1. write objects to the database in this manner
  2. use the version: "*" attribute in my storage write request object to only create these game artifacts once?

Any help is welcome and appreciated.
Thank you!

  1. Versions: Nakama 3.10.0, Docker, Unity 2021.1.11f
  2. Server Framework Runtime language: TS/JS
  3. Cockroach DB image version: cockroachdb/cockroach:latest-v21.2

Hi @lightning, are you running this concurrently and do you have contention on these objects?

This error might happen if you are doing non-OCC concurrent writes to the same object, and it’s something we will improve in the future.

As for your second question, you can just add version: "*" to your write, and it will return an error if it already exists.

Hi @ftkg !
Thank you for your quick reply :smile:

How do I know if I am doing “non-OCC” concurrent writes to the same object?

I am following the guide provided in the documentation to set up my project and make these database calls. I’m fairly certain I did not change any configuration other than what was outlined.

If I am doing non-OCC concurrent write to the same object, how do I not? :laughing:

Thank you!

I think you need to basically check if you’re calling this function multiple times and if they may be running at the same time. :slight_smile:

thank you, @ftkg

i figured out that the example code from the documentation was throwing an error, where version: '*' throws an error if something is already created - as you stated.

after taking out the code that throws an error, the data goes in the database perfectly.

const writeObjects: nkruntime.StorageWriteRequest[] =
        TREASURES
            .map(treasure => {
                // create a write request object for each Treasure
                const writeRequest: nkruntime.StorageWriteRequest = {
                    userId: "00000000-0000-0000-0000-000000000000",
                    collection: TREASURES_COLLECTION_NAME,
                    key: `${treasure.treasureId}`,
                    permissionRead: TREASURES_PERMISSION_READ,
                    permissionWrite: TREASURES_PERMISSION_WRITE,
                    value: treasure,
                };
                return writeRequest;
            });
try {
        nk.storageWrite([...writeObjects]);
    } catch (error) {
        logger.error('Treasures storageWrite error: %q', error);
        // there was an error being thrown here... removed this
    }

thanks!