Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get() returns undefined when no data is returned by API #66

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/contract/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ 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,6 +190,10 @@ 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) {
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
Loading