# Return empty when write in db and try to read with ReadStorageObjectsAsync

**URL:** https://forum.heroiclabs.com/t/return-empty-when-write-in-db-and-try-to-read-with-readstorageobjectsasync/4576
**Category:** Client Libraries
**Created:** [November 9, 2023, 9:43pm UTC](https://forum.heroiclabs.com/t/return-empty-when-write-in-db-and-try-to-read-with-readstorageobjectsasync/4576 "2023-11-09T21:43:54Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![gabrielmontoto](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/gabrielmontoto/32/1890_2.png) [@gabrielmontoto](https://forum.heroiclabs.com/u/gabrielmontoto)
#### Post date: [November 9, 2023, 9:43pm UTC](https://forum.heroiclabs.com/t/return-empty-when-write-in-db-and-try-to-read-with-readstorageobjectsasync/4576/1 "2023-11-09T21:43:54Z")

</div>

Hello,  
I’m working on a process that calls an rpc that reads and writes to the database to update storage (a monster) information in the game I’m developing.

The game has a screen where it’s possible to update some monster stats like health, strength, etc. With the rpc call, the database is updated, and these stats are correctly displayed on that screen to the player.

The problem arises when the player tries to use an item that allows them to redistribute points. This item makes a call to the server to reset the stats to their initial values (which are random for each monster). The call to reset works perfectly, but the read, using `ReadStorageObjectsAsync`, returns an empty array, even though the same call is made for the point distribution.

I am using client version 3.9.0 and backend version 3.16.0

1. Code used to make the ReadStorageObjectsAsync call

```auto
 private async UniTask<IEnumerable<MonsterDbo>> LoadMonstersFromDb(string[] monstersIds)
        {
            List<MonsterDbo> monsters = new List<MonsterDbo>();
            const string monstersCollection = "Monsters";
            IApiStorageObjects storageObjectList = null;
            StorageObjectId[] ids = new StorageObjectId[monstersIds.Count()];

            for (int i = 0; i < monstersIds.Length; i++)
            {
                ids[i] = new StorageObjectId { Collection = monstersCollection, Key = "nftId-" + monstersIds[i] };
            }

            try
            {
                storageObjectList = await _client.ReadStorageObjectsAsync(_session, ids);
            }
            catch (Exception exception)
            {
                ErrorResponse(exception.Message);
            }
            ...
        }

```

1. Output logs when there is a write, of the stats on the monster.

```auto
backend-nakama-1 {"level":"info","ts":"2023-11-09T20:15:11.992Z","caller":"server/runtime_javascript_logger.go:74","msg":"file: main.ts:387 handleBeforeReadStorageObjects"...}
backend-nakama-1 {"level":"info","ts":"2023-11-09T20:15:11.992Z","caller":"server/runtime_javascript_logger.go:74","msg":"file: main.ts:388 ctx map[string]interface {}{\"..."}
backend-nakama-1 {"level":"info","ts":"2023-11-09T20:15:11.992Z","caller":"server/runtime_javascript_logger.go:74","msg":"file: main.ts:389 data map[string]interface {}{\"objectIds\":[]interface {}{map[string]interface {}{\"collection\":\"Monsters\", \"key\":\"nftId-2901\"}}}"...}
backend-nakama-1 {"level":"info","ts":"2023-11-09T20:15:11.992Z","caller":"server/runtime_javascript_logger.go:74","msg":"file: main.ts:390 handleBeforeReadStorageObjects FINISHED"...}
backend-nakama-1 {"level":"info","ts":"2023-11-09T20:15:11.993Z","caller":"server/runtime_javascript_logger.go:74","msg":"file: main.ts:402 handleAfterReadStorageObjects"...}
backend-nakama-1 {"level":"info","ts":"2023-11-09T20:15:11.993Z","caller":"server/runtime_javascript_logger.go:74","msg":"file: main.ts:403 ctx map[string]interface {}{\"..."}
backend-nakama-1 {"level":"info","ts":"2023-11-09T20:15:11.993Z","caller":"server/runtime_javascript_logger.go:74","msg":"file: main.ts:404 data map[string]interface {}{\"objects\":[]interface {}{map[string]interface {}{\"collection\":\"Monsters\", \"createTime\":\"2023-06-20T11:51:56Z\", \"key\":\"nftId-2901\", \"permissionRead\":2, \"updateTime\":\"2023-11-09T20:15:11Z\", \"userId\":\"00000000-0000-0000-0000-000000000000\",...}
backend-nakama-1 {"level":"info","ts":"2023-11-09T20:15:11.993Z","caller":"server/runtime_javascript_logger.go:74","msg":"file: main.ts:405 request map[string]interface {}{\"objectIds\":[]interface {}{map[string]interface {}{\"collection\":\"Monsters\", \"key\":\"nftId-2901\"}}}"...}
backend-nakama-1 {"level":"info","ts":"2023-11-09T20:15:11.993Z","caller":"server/runtime_javascript_logger.go:74","msg":"file: main.ts:406 handleAfterReadStorageObjects FINISHED"...}

```

1. Output logs when there is a read, when the error occurs.

```auto
backend-nakama-1 {"level":"info","ts":"2023-11-09T20:44:43.685Z","caller":"server/runtime_javascript_logger.go:74","msg":"file: main.ts:387 handleBeforeReadStorageObjects"...}
backend-nakama-1 {"level":"info","ts":"2023-11-09T20:44:43.685Z","caller":"server/runtime_javascript_logger.go:74","msg":"file: main.ts:388 ctx map[string]interface {}{\"..."
backend-nakama-1 {"level":"info","ts":"2023-11-09T20:44:43.685Z","caller":"server/runtime_javascript_logger.go:74","msg":"file: main.ts:389 data map[string]interface {}{\"objectIds\":[]interface {}{map[string]interface {}{\"collection\":\"Monsters\", \"key\":\"nftId-2901\"}}}"...}
backend-nakama-1 {"level":"info","ts":"2023-11-09T20:44:43.685Z","caller":"server/runtime_javascript_logger.go:74","msg":"file: main.ts:390 handleBeforeReadStorageObjects FINISHED"...}
backend-nakama-1 {"level":"info","ts":"2023-11-09T20:44:43.685Z","caller":"server/runtime_javascript_logger.go:74","msg":"file: main.ts:402 handleAfterReadStorageObjects"...}
backend-nakama-1 {"level":"info","ts":"2023-11-09T20:44:43.685Z","caller":"server/runtime_javascript_logger.go:74","msg":"file: main.ts:403 ctx map[string]interface {}{\"..."}
backend-nakama-1 {"level":"info","ts":"2023-11-09T20:44:43.685Z","caller":"server/runtime_javascript_logger.go:74","msg":"file: main.ts:404 data map[string]interface {}{}","api_id":"readstorageobjects","mode":"after"}
backend-nakama-1 {"level":"info","ts":"2023-11-09T20:44:43.685Z","caller":"server/runtime_javascript_logger.go:74","msg":"file: main.ts:405 request map[string]interface {}{\"objectIds\":[]interface {}{map[string]interface {}{\"collection\":\"Monsters\", \"key\":\"nftId-2901\"}}}"...}
backend-nakama-1 {"level":"info","ts":"2023-11-09T20:44:43.685Z","caller":"server/runtime_javascript_logger.go:74","msg":"file: main.ts:406 handleAfterReadStorageObjects FINISHED"...}

```

---

<div class="post-metadata">

### Author: ![gabrielmontoto](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/gabrielmontoto/32/1890_2.png) [@gabrielmontoto](https://forum.heroiclabs.com/u/gabrielmontoto)
#### Post date: [November 9, 2023, 9:45pm UTC](https://forum.heroiclabs.com/t/return-empty-when-write-in-db-and-try-to-read-with-readstorageobjectsasync/4576/2 "2023-11-09T21:45:56Z")

</div>

Image of the first point showing the result of ReadStorageObjectsAsync when it works fine.

 ![image working fine](https://us1.discourse-cdn.com/flex020/uploads/heroiclabs/original/2X/e/ed959eb10d247d3fbf49d696e45cb90d417d3eea.png)

---

<div class="post-metadata">

### Author: ![gabrielmontoto](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/gabrielmontoto/32/1890_2.png) [@gabrielmontoto](https://forum.heroiclabs.com/u/gabrielmontoto)
#### Post date: [November 9, 2023, 9:47pm UTC](https://forum.heroiclabs.com/t/return-empty-when-write-in-db-and-try-to-read-with-readstorageobjectsasync/4576/3 "2023-11-09T21:47:06Z")

</div>

Image of the second point showing the result of ReadStorageObjectsAsync when the error occurs

 ![image error](https://us1.discourse-cdn.com/flex020/uploads/heroiclabs/original/2X/e/ec2c5ba720b3b1121e1d3c80dbad90867c6ef3a2.png)

---

<div class="post-metadata">

### Author: ![gabrielmontoto](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/gabrielmontoto/32/1890_2.png) [@gabrielmontoto](https://forum.heroiclabs.com/u/gabrielmontoto)
#### Post date: [November 13, 2023, 2:27pm UTC](https://forum.heroiclabs.com/t/return-empty-when-write-in-db-and-try-to-read-with-readstorageobjectsasync/4576/4 "2023-11-13T14:27:18Z")

</div>

Updating Nakama version to 3.19 and Nakama common to 1.30 still gives the same error

---

<div class="post-metadata">

### Author: ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)
#### Post date: [November 13, 2023, 2:49pm UTC](https://forum.heroiclabs.com/t/return-empty-when-write-in-db-and-try-to-read-with-readstorageobjectsasync/4576/5 "2023-11-13T14:49:46Z")

</div>

Hello @gabrielmontoto,

Please share the code snippet for how you’re writing the object.

---

<div class="post-metadata">

### Author: ![gabrielmontoto](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/gabrielmontoto/32/1890_2.png) [@gabrielmontoto](https://forum.heroiclabs.com/u/gabrielmontoto)
#### Post date: [November 13, 2023, 6:05pm UTC](https://forum.heroiclabs.com/t/return-empty-when-write-in-db-and-try-to-read-with-readstorageobjectsasync/4576/7 "2023-11-13T18:05:12Z")

</div>

Hi, here is the code:

```auto
const outBattleUseItemEffects: IdMap<ItemOutOfBattleEffect> = {
        [ItemEffectId.RedistributePointsEffect]: {
            outOfBattleEffect: redistributePointsOutOfBattleEffect,
        },
    }
function redistributePointsOutOfBattleEffect(
        ctx: nkruntime.Context,
        nk: nkruntime.Nakama,
        logger: nkruntime.Logger,
        item: Item,
        amount: number,
        monster?: Monster
    ): MultiUpdateWrite {
        let canUse = false

        if (!monster) throw MONSTER_NOT_FOUND
        let currentMonster: Monster = monster

        for (let i = 0; i < currentMonster.attributes.length; i++) {
            if (currentMonster.attributes[i].baseValue !== currentMonster.attributes[i].initialValue) {
                canUse = true
            }
        }

        if (!canUse) throw REDISTRIBUTE_POINTS_CANNOT_USE

        for (let i = 0; i < currentMonster.attributes.length; i++) {
            currentMonster.attributes[i].baseValue = currentMonster.attributes[i].initialValue
            currentMonster.attributes[i].currentValue = currentMonster.attributes[i].initialValue
        }

        const writeKey: nkruntime.StorageWriteRequest = {
            collection: monstersCollectionName,
            key: keyPrefixNft + currentMonster.nftId,
            userId: globalUserId,
            value: currentMonster,
        }

        return {
            accountUpdate: null,
            storageWriteKeys: [writeKey],
            walletUpdate: [],
        }
    }

```

second part

```auto
function useItemRpc(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, payload: string): string {
    if (!ctx.userId) {
        throw USE_ITEM_USER_ID_UNDEFINED
    }

    if (!payload) {
        throw USE_ITEM_PAYLOAD_UNDEFINED
    }

    let parsedPayload: UseItemPayload = JSON.parse(payload) as UseItemPayload

    let itemQuantity = parsedPayload.quantity || 1
    if (itemQuantity == 0) itemQuantity = 1

    const removeItemQuantity = Math.abs(itemQuantity) * -1
    const removeItemStack: ItemStack = {
        quantity: removeItemQuantity,
        targetId: parsedPayload.itemId,
    }

    let multiUpdateKeys = outBattleUseItemEffects[parsedPayload.effectId].outOfBattleEffect(ctx, nk, logger, item, itemQuantity, monster)

    const itemChangeset = ItemStackChangesetWriteKeys(ctx.userId, nk, [removeItemStack], logger) // This is a inventory/item changeset

    if (!itemChangeset) {
        throw ITEM_EFFECT_DOES_NOT_HAVE_ITEM
    }

    multiUpdateKeys.storageWriteKeys.push(...itemChangeset)

    nk.multiUpdate(null, multiUpdateKeys.storageWriteKeys, multiUpdateKeys.walletUpdate)

    response.success = true
    return JSON.stringify(response)
}
```

---

<div class="post-metadata">

### Author: ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)
#### Post date: [November 14, 2023, 1:38pm UTC](https://forum.heroiclabs.com/t/return-empty-when-write-in-db-and-try-to-read-with-readstorageobjectsasync/4576/8 "2023-11-14T13:38:08Z")

</div>

Could you please work out a single test case or code to reproduce this? We’ve not encountered this issue before nor am I able to reproduce it.
