storageIndexList returns undefined objects

I’m trying to create a Storage List Index following the documentation for typescript:

I have registered the Storage index in the InitModule with the following code:

‘’’
const name = ‘TestIndex’;
const collection = ‘TestCollection’;
const key = ‘TestKey’; // Set to empty string to match all keys instead
const fields = [‘field1’, ‘field2’]; // Only objects containing any of these keys and respective values are indexed.
const maxEntries = 1000;
const indexOnly = false;

const err = initializer.registerStorageIndex(
name,
collection,
key,
fields,
fields,
maxEntries,
indexOnly
);
‘’’

I have then created two rpcs, one for creating a storage object and one for listing the storage objects using storageIndexList:

‘’’
let RPC_CreateTestStorage: nkruntime.RpcFunction = function (
context: nkruntime.Context,
logger: nkruntime.Logger,
nk: nkruntime.Nakama,
payload: string
) {
let storageObject: nkruntime.StorageWriteRequest = [
{
collection: ‘TestCollection’,
key: ‘TestKey’,
userId: context.userId,
value: { field1: 1, field2: ‘foo’ },
},
];

try {
nk.storageWrite(storageObject);
} catch (error) {
throw Error(‘Could not write storage obj’ + error);
}
};

let RPC_ReadTestStorage: nkruntime.RpcFunction = function (
context: nkruntime.Context,
logger: nkruntime.Logger,
nk: nkruntime.Nakama,
payload: string
) {
const name = ‘TestIndex’;
const query = ‘+value.field1:1 value.field2:foo’;
const limit = 10;

let storageObjs: nkruntime.StorageObject = nk.storageIndexList(
name,
query,
limit
);
return storageObjs[0].createTime.toString();
};
‘’’

I can create the storage objects without problems but reading them returns the following error:
“rpc error: code = Internal desc = TypeError: Cannot read property ‘createTime’ of undefined at RPC_ReadTestStorage (index.js:522:27(17))”

Since the code is just copied from the Documentation I can’t figure out what the issue might be.

Hello @Linus,

I think your TS definitions are outdated and you’re using a more recent version of Nakama, your function should look like this:

let RPC_ReadTestStorage: nkruntime.RpcFunction = function (
    context: nkruntime.Context,
    logger: nkruntime.Logger,
    nk: nkruntime.Nakama,
    payload: string
) {
    const name = 'TestIndex';
    const query = '+value.field1:1 value.field2:foo';
    const limit = 10;

    let storageObjs: nkruntime.StorageIndexResult = nk.storageIndexList(
        name,
        query,
        limit
    );
    return storageObjs.objects[0].createTime.toString();
};

Please update your TS definitions to latest and Nakama as well to v3.25.0 (if you’re running an older version).

Best.

1 Like