EOSIO / eos

An open source smart contract platform
https://developers.eos.io/manuals/eos
MIT License
11.28k stars 3.6k forks source link

multi index data cannot find in get table rows #4751

Closed Hitmanv closed 6 years ago

Hitmanv commented 6 years ago

but I can read it from contract i'm using the local test net i deployed the contract

cleos set contract hitman contract_path c.wast c.abi  -p hitman@active

and the query

cleos get table hitman hitman table_name

or in eosjs

eos.getTableRows({ code: "hitman", scope: "hitman",  json: true, table: table_name })

the contract code looks like this

#include <eosiolib/eosio.hpp>

using namespace eosio;

class hello : public eosio::contract {
  /// @abi table tests
  struct t {
    uint64_t id;
    auto primary_key() const { return id; }
    EOSLIB_SERIALIZE(t, (id))
  };
  typedef multi_index<N(t), t> tests;

  public:
      using contract::contract;
      /// @abi action 
      void hi( account_name user ) {
        tests t_table(_self, _self);
        t_table.emplace(_self, [&](auto &r){
          r.id = 1;
        });
      }

      /// @abi action
      void get() {
        tests t_table(_self, _self);
        auto itr = t_table.begin();
        // it's ok here
        print(itr->id);
      }
};

EOSIO_ABI( hello, (hi)(get) )
taokayan commented 6 years ago

Depending on your source code and actual usage, a table will only be created when it's being used.

mmcs85 commented 6 years ago

Not totaly sure but shouldn't this line: typedef multi_index<N(t), t> tests;

Change to: typedef multi_index<N(tests), t> tests;

The multi_index key needs to match the abi definition

taokayan commented 6 years ago

we recommend to be typedef multi_index<N(t), t> tests;. In this case your table name is t.

Hitmanv commented 6 years ago

that's it. thank you all!

SheharyarCh commented 6 years ago

it is returning value that we stored in hi method manually. How to retrieve complete record by giving an id or name?

SheharyarCh commented 6 years ago

I have used this code to store the data(name and marks) in the table, now i want to retrieve that data in the form of record. how can i get that?

void addmarks(account_name examiner, account_name student, uint64_t marks) { auto toitr = _accounts.find(student); if(toitr == _accounts.end()) { _accounts.emplace(examiner, [&](auto& st) { st.name = student; st.obtainedmarks = marks; }); } else { _accounts.modify(toitr,0,[&](auto& st) { st.obtainedmarks += marks; eosio_assert(st.obtainedmarks >= marks, "Marks Assigned"); }); } }

kindly help me to solve it?

Hitmanv commented 6 years ago

@SheharyarCh did you mean the index or primary key? you could use get/find(primary_key) or use the get_index method reference