EOSIO / eosjs

General purpose library for the EOSIO blockchain.
http://eosio.github.io/eosjs
MIT License
1.43k stars 463 forks source link

some err about get_table_rows #154

Closed songsongMr closed 6 years ago

songsongMr commented 6 years ago

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

xiangxn commented 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));
blackwatertepes commented 6 years ago

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...

  1. 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.

  2. 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.

samkirton commented 6 years ago

This post is gold, thank you @blackwatertepes

ericqweinstein commented 5 years ago

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.

jeffreyssmith2nd commented 5 years ago

Its a part of the JsonRpc class now: https://github.com/EOSIO/eosjs/blob/master/src/eosjs-jsonrpc.ts#L136

ericqweinstein commented 5 years ago

IIIII'm dumb. Thanks so much!