From 27756ee22aeb5d1a12da343e30af03ce912fce3c Mon Sep 17 00:00:00 2001 From: dafuga Date: Thu, 4 Jan 2024 19:26:20 -0500 Subject: [PATCH] fix: get() returns undefined when no data is returned --- src/contract/table.ts | 9 ++++++++- ...414317c9310901aea8c456060b14733cc332e56a.json | 16 ++++++++++++++++ test/tests/table.ts | 10 ++++++++-- 3 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 test/data/requests/414317c9310901aea8c456060b14733cc332e56a.json diff --git a/src/contract/table.ts b/src/contract/table.ts index 53f9ba4..b204d1c 100644 --- a/src/contract/table.ts +++ b/src/contract/table.ts @@ -155,7 +155,10 @@ 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, @@ -187,6 +190,10 @@ export class Table { } const {rows} = await this.client!.v1.chain.get_table_rows(tableRowsParams) + + if (rows.length === 0) { + return undefined + } let [row] = rows if (!this.rowType) { diff --git a/test/data/requests/414317c9310901aea8c456060b14733cc332e56a.json b/test/data/requests/414317c9310901aea8c456060b14733cc332e56a.json new file mode 100644 index 0000000..adf437b --- /dev/null +++ b/test/data/requests/414317c9310901aea8c456060b14733cc332e56a.json @@ -0,0 +1,16 @@ +{ + "request": { + "path": "https://eos.greymass.com/v1/chain/get_table_rows", + "params": { + "method": "POST", + "body": "{\"table\":\"producers\",\"code\":\"eosio\",\"scope\":\"eosio\",\"limit\":1,\"lower_bound\":\"doesnotexist\",\"upper_bound\":\"doesnotexist\",\"key_type\":\"name\",\"json\":false}" + } + }, + "status": 200, + "json": { + "rows": [], + "more": false, + "next_key": "" + }, + "text": "{\"rows\":[],\"more\":false,\"next_key\":\"\"}" +} \ No newline at end of file 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', () => {