diff --git a/src/contract/table.ts b/src/contract/table.ts index 53f9ba4..dd10925 100644 --- a/src/contract/table.ts +++ b/src/contract/table.ts @@ -155,7 +155,7 @@ export class Table { * Each key-value pair in the queryParams object corresponds to a field and its expected value in the table. * @returns {Promise} Promise resolving to a single table row. */ - async get(value?: API.v1.TableIndexType | string, params: QueryParams = {}): Promise { + async get(value?: API.v1.TableIndexType | string, params: QueryParams = {}): Promise { const tableRowsParams: any = { table: this.name, code: this.account, @@ -186,8 +186,21 @@ export class Table { tableRowsParams.index_position = fieldToIndexMapping[params.index].index_position } - const {rows} = await this.client!.v1.chain.get_table_rows(tableRowsParams) - let [row] = rows + let row + + try { + const {rows} = await this.client!.v1.chain.get_table_rows(tableRowsParams) + + row = rows[0] + } catch (error: unknown) { + const message = (error as { message }).message + + if (message.includes('No data')) { + return + } else { + throw error + } + } if (!this.rowType) { row = Serializer.decode({ diff --git a/test/tests/table.ts b/test/tests/table.ts index 3b2f828..150e1c4 100644 --- a/test/tests/table.ts +++ b/test/tests/table.ts @@ -115,8 +115,8 @@ suite('Table', () => { assert.instanceOf(table, Table) const row = await table.get() assert.instanceOf(row, NameBid) - assert.instanceOf(row.newname, Name) - assert.instanceOf(row.high_bid, Int64) + assert.instanceOf(row?.newname, Name) + assert.instanceOf(row?.high_bid, Int64) }) suite('all', () => { test('should return every single row in a table', async () => { @@ -388,6 +388,12 @@ suite('Table', () => { const result = await contract.table('accounts', 'wharfkittest').get() assert.instanceOf(result.balance, Asset) }) + + test('should return undefined when no entry is found', async function () { + const row = await producersTable.get('doesnotexist') + + assert.isUndefined(row) + }) }) suite('first', () => {