EOSIO / eosjs

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

How to fetch EOS database table data using eosjs? #146

Closed vicraft18 closed 6 years ago

vicraft18 commented 6 years ago

Inside eosjs I have the following: Eos = require('eosjs') // Eos = require('./src') config = { keyProvider: ["5JiGSaDukbHn9A7LRNgnaLmLsGkHW6UVUtG1232tpxh5WJ6x8","5JrArfC9rVreYJddcUg8mwdasdUPCn432uoYJUvho1XMvVLQ","5JrArgsd9rVreYJddcUg8mwdvkbBbFUPCn432uoYJUvho1XMvVLQ"], httpEndpoint: 'http://192.168.109.134:8888', chainId: "0000000000000000000000000000000000000000000000000000000000000000" } eos = Eos.Localnet(config);

eos.contract('victor3').then(contract => {

contract.getdata('123', {authorization: 'victor3'}).then(result => {
        console.log(result.transaction.transaction.actions);
        console.log(result);
})

});

In my contract I have something that is something like this: void getdata(uint64_t post_id){ das datable(_self, _self); auto post_da = datable.find( post_id); eosio::print("Post_id: ", post_da->post_id, " Post_Tile: ", post_da->title.c_str(), " Content: ", post_da->content.c_str()); }

I noticed that changing the return type of getdata is not the way to go, if that's the case, how do I return data from this contract function?

jcalfee commented 6 years ago

Related: https://github.com/EOSIO/eosjs/issues/70

Does that clear things up?

Aluman commented 6 years ago

I have the same question. From related question( #70) I realize that I should get transaction, but I can't find necessary method from api. cleos can find transaction: cleos get transaction 113b2d0167121e9db2676ab04dec74178e6be979dd373306929c652a95bf078e

Where I should store function results? I try change contract to:

#include <eosiolib/eosio.hpp>

using namespace eosio;

class hello : public eosio::contract {
  public:
      using contract::contract;

      /// @abi action 
      bool hi( account_name user ) {
         print( "Hello, ", name{user} );
         return false;
      }
};

EOSIO_ABI( hello, (hi))

But I can't generate abi file for that contract:

error: no matching function for call to 'execute_action'
EOSIO_ABI( hello, (hi))
^~~~~~~~~~~~~~~~~~~~~~~
/usr/local/include/eosiolib/dispatcher.hpp:85:13: note: expanded from macro 'EOSIO_ABI'
            EOSIO_API( TYPE, MEMBERS ) \
            ^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/include/eosiolib/dispatcher.hpp:72:27: note: expanded from macro 'EOSIO_API'
   BOOST_PP_SEQ_FOR_EACH( EOSIO_API_CALL, TYPE, MEMBERS )
   ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/aluman/opt/boost_1_67_0/include/boost/preprocessor/seq/for_each.hpp:29:88: note: expanded from macro 'BOOST_PP_SEQ_FOR_EACH'
#    define BOOST_PP_SEQ_FOR_EACH(macro, data, seq) BOOST_PP_SEQ_FOR_EACH_DETAIL_CHECK(macro, data, seq)
                                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
note: (skipping 6 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
/home/aluman/opt/boost_1_67_0/include/boost/preprocessor/seq/for_each.hpp:78:76: note: expanded from macro 'BOOST_PP_SEQ_FOR_EACH_M_IM'
#    define BOOST_PP_SEQ_FOR_EACH_M_IM(r, im) BOOST_PP_SEQ_FOR_EACH_M_I(r, im)
                                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
/home/aluman/opt/boost_1_67_0/include/boost/preprocessor/seq/for_each.hpp:83:61: note: expanded from macro 'BOOST_PP_SEQ_FOR_EACH_M_I'
# define BOOST_PP_SEQ_FOR_EACH_M_I(r, macro, data, seq, sz) macro(r, data, BOOST_PP_SEQ_HEAD(seq))
                                                            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/include/eosiolib/dispatcher.hpp:68:7: note: expanded from macro 'EOSIO_API_CALL'
      eosio::execute_action( &thiscontract, &OP::elem ); \
      ^~~~~~~~~~~~~~~~~~~~~
/usr/local/include/eosiolib/dispatcher.hpp:41:9: note: candidate template ignored: could not match 'void' against 'bool'
   bool execute_action( T* obj, void (Q::*func)(Args...)  ) {
        ^
1 error generated.
vicraft18 commented 6 years ago

@jcalfee I have some concern about this, let's suppose i'm searching for some very specific data, at my web server side I can only use RPC or eosjs to query the table

eos.getTableRows(true,'victor3','victor3','data',0,0,1000,1000).then(function (_data) {
for( var j = 0; j < _data.rows.length; j++) { //do something } })

I was just wondering there a better way to do database query than this? This method will certainly crash if data inside db table grow.

vicraft18 commented 6 years ago

Storing the data inside a table, then right away fetches the table, is this the only way to go or is there some future plan?