Below is my smart contract:
#include <eosio/eosio.hpp>
#include <eosio/print.hpp>
#include <eosio/system.hpp>
#include <string>
using namespace eosio;
using std::string;
class [[eosio::contract("document")]] document : public eosio::contract {
public:
using contract::contract;
document(name receiver, name code, datastream<const char*> ds):contract(receiver, code, ds) {}
[[eosio::action]]
void erasedoc(name key) {
document_index documents( get_self(), get_first_receiver().value);
bool found = false;
for(auto itrer = documents.begin(); itrer != documents.end() && found!=true; itrer++) {
if(itrer->key == key) {
found = true;
documents.erase(itrer);
}
}
check(found == false, "Document does not exist");
}
private:
struct [[eosio::table]] documenttable {
uint64_t doc_id;
name key;
std::string doc_name;
std::string doc_description;
uint64_t primary_key() const { return doc_id;}
};
typedef eosio::multi_index<"documents"_n, documenttable> document_index;
};
doc_id
is the primary key. But I want to delete the record using key
. As, key is not the primary key I cannot use auto iterator = documents.find(key.value);
. So, I implemented this logic. But this returns errors.
Error:
Error 3160005: The table operation is not allowed
Error Details:
dereference of deleted object
pending console output:
Please help. How could I delete the record using key
?