StorageRead not behaving as expected

Hello folks, I’ve been reading on the storage documentation and do not seem to get this working for my game.
I’m writing this object to the storage (note I even added a public read). Each player should submit a caption for a given photo.
image
Once all the N players have submitted their captions, I should have N values for key “matchState.CurrentImageId” in the “captions” collection. This part is behaving as expected, and I can see the collection with the right key on the admin console.

When the last player submits their caption I need to lookup all the captions for key “matchState.CurrentImageId” inside “captions” and send them back to the client. Based on the documentation, I think Storage Read seems like the perfect function for this. Since I need all of the objects in this collection and key I’m not passing a UserId on my storageRead object.
image

The output of this Storage Read is an empty array, I thought because this is a call from the server it should have access to all the data in the collection. What I expected as the output of this function was to get the N elements inside the “captions” collection that have the key"matchState.CurrentImageId".

I did also try adding a UserId to my storage object but I only get the object that belongs to the current player calling the function which is not what I want.

I also tried using StorageList which does work fine but I get back all the objects in the collection. In this case I’m loading a bunch of data that I do not need, I only need the ones with key: “matchState.CurrentImageId”
image

Could you explain me what I’m doing wrong? I thought StorageRead from the server has access to all the objects on a collection similar to how StorageList works.

  1. Versions: Nakama 3.17.1, Windows, Docker
  2. Server Framework Runtime language (If relevant) go 1.20

:tv: Media:

Hello @rpparede,

This is expected behaviour, storageRead attempts to retrieve a storage object per storage read object - the collection, key and user_id must match the target storage objects, they have to be a 1 to 1 match. However when called from the server the read permissions do not apply.

StorageList will list all keys in the collection, perhaps you should consider modeling your data in a different way so this other data is in a separate collection.

Best.

1 Like

Thanks for your reply @sesposito .This still feels a bit off to me. Please help me understand the following case and how to model my data in a nakama-fiendly way.

Let’s consider the scenario where I want to save the gameStatus on storage. GameStatus keeps track of some asset ids during the match. GameStatus is part of the “Games” collection with key “MatchId”.

GameStatus
{
“gameId1”:{“id1”,“id2”}
}
I’m failing to understand why I need to link this object that is shared between multiple players in the match with a single userId. Since the key I am using is unique I should be able to simply read the object from any player call by simply using the collection and the key. Using storageList seems a bit wastful since I’m loading a lot of data just to iterate and find a single element (which I’m currently already doing somewhere else).
Would appreciate any input here, thanks!

I think I just answered my own question. So I can write an object without a userId (which defaults to 000-000-000… userId). Then I can ready this object anywhere else independently of the current userId making the call. In my case GameState can be written without explicitly specifying a userId and then any player can have access to update that. Just dropping it here in case someone else gets confused on the same part :slight_smile: Thanks!