0

I have a table defined as the following:

struct [[ eosio::table, eosio::contract("voting") ]] identity
{
    uint64_t key;
    key256 identityHash;
    time_point_sec createdAt;

    uint64_t primary_key() const { return key; }
    key256 by_hash() const { return identityHash; }
};
typedef multi_index<"identity"_n, identity,
    indexed_by< "by_hash"_n, const_mem_fun<identity, key256, &identity::by_hash> >
> identity_index;

I now want to use .find on the table using the secondary index but I am unable to do so. I am trying the following:

voting::identity_index identities(_self, _self.value);
auto itr = identities.find(out);
eosio_assert(itr == identities.end(), "User already registered to vote!");

But the above code just searches on the primary key. Is there a way to search on the secondary key?

1

To use the secondary index, you need to use get_index().

voting::identity_index identities(_self, _self.value);
auto idx = identities.get_index<"by_hash"_n>();
auto itr = idx.find(out);
eosio_assert(itr == identities.end(), "User already registered to vote!");
| improve this answer | |
0

See https://developers.eos.io/eosio-cpp/docs/using-multi-index-tables for more info

It might not be possible to use .find on the second index, but you could always iterate through each element and compare relevant values. I believe runtime should be the same.

From the above example:

std::vector<uint64_t> keysForDeletionFromVotes;
    // find items which are for the named poll
    for(auto& item : _votes) {
        if (item.pollName == pollName) {
            keysForDeletionFromVotes.push_back(item.key);   
        }
    }

Related answer: Multiple parameters for search

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.