Skip to content

Commit

Permalink
fix: get() returns undefined when no data is returned
Browse files Browse the repository at this point in the history
  • Loading branch information
dafuga committed Jan 5, 2024
1 parent 40c5c30 commit bf2222a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/contract/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export class Table<RowType = any> {
* Each key-value pair in the queryParams object corresponds to a field and its expected value in the table.
* @returns {Promise<TableRow>} Promise resolving to a single table row.
*/
async get(value?: API.v1.TableIndexType | string, params: QueryParams = {}): Promise<RowType> {
async get(value?: API.v1.TableIndexType | string, params: QueryParams = {}): Promise<RowType | undefined> {
const tableRowsParams: any = {
table: this.name,
code: this.account,
Expand Down Expand Up @@ -187,8 +187,13 @@ export class Table<RowType = any> {
}

const {rows} = await this.client!.v1.chain.get_table_rows(tableRowsParams)

if (rows.length === 0) {
return undefined
}
let [row] = rows


if (!this.rowType) {
row = Serializer.decode({
data: row,
Expand Down
16 changes: 16 additions & 0 deletions test/data/requests/414317c9310901aea8c456060b14733cc332e56a.json
Original file line number Diff line number Diff line change
@@ -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\":\"\"}"
}
10 changes: 8 additions & 2 deletions test/tests/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit bf2222a

Please sign in to comment.