Closed songsongMr closed 6 years ago
eos.getTableRows({
scope: argv.a,
code: argv.a,
table: T_TABLE_NAME,
json: true,
lower_bound: u_name,
limit: 1
}).then(result => {
resolve(result);
}).catch(err => reject(err));
In case anyone else has struggled with this like I have, I'd love to post my findings about how this works, as the documentation is a bit lacking, atm. Your example @xiangxn is close, but has a few issues...
The upper bound is missing, and should be included. This doesn't matter when the record exists, as limit: 1
will return the correct result. However, if the record does not exist, the next record, after the lower bound specified will be returned, resulting in a false positive.
u_name
doesn't give clarity to the argument that should be passed in. The lower_bound parameter needs to be the account name encoded as an Integer, in Big Endian, as a String. And, since Javascript has issues with integers larger than 2^53 you'll want to use a library, such as BigNumber.js
Here's an example of what worked for me...
const table_key = new BigNumber(eos.format.encodeName(ACCOUNT_NAME, false))
const accounts = await eos.getTableRows({
code: CONTRACT_NAME,
json: true,
limit: 1,
lower_bound: table_key.toString(),
scope: CONTRACT_NAME,
table: TABLE_NAME,
upper_bound: table_key.plus(1).toString()
})
const balance = accounts.rows[0].balance
encodeName
can be found within Eos.modules, and excepts little_endian:Boolean as the second argument.
This post is gold, thank you @blackwatertepes
Is getTableRows
still part of the API? Maybe I'm extra dumb today, but I can't find it in the codebase or as a method on either the default eosjs
export or the Api
.
Its a part of the JsonRpc class now: https://github.com/EOSIO/eosjs/blob/master/src/eosjs-jsonrpc.ts#L136
IIIII'm dumb. Thanks so much!
query table like this : eos.getTableRows(true,code,index,table) can be success. but how do I call the method of get_table_rows
if I call it like this: eos.get_table_rows(false,'codeName','scopeName','tableName','3','0','-1','10');//query where key==3 Then prompt error code is 500