1

I have written one smart contract for insert, update and delete data. The smart contract is compiled and deployed to the two accounts. I insert data using calling the action upsert, this transaction was successful. Same I delete data using action erase, this transaction also gets success. but when I try to get data using

cleos get table varshaccount member people

and

cleos get table memberacc member people

it returns empty. Below is my code:

#include <eosio/eosio.hpp>
#include <eosio/print.hpp>
#include <string>

using namespace eosio;
using std::string;

class [[eosio::contract("member")]] member : public eosio::contract {

        public:

        using contract::contract;

        member(name receiver, name code, datastream<const char*> ds):contract(receiver, code, ds) {}

        [[eosio::action]]
                void upsert(name user, std::string first_name, std::string last_name, std::string contact, std::string address) {
                        require_auth( user );
                        member_index addresses( get_self(), get_first_receiver().value );
                        auto iterator = addresses.find(user.value);
                        if( iterator == addresses.end() )
                        {
                                //user is not in table so add new record
                                addresses.emplace(user, [&]( auto& row ) {
                                                row.key = user;
                                                row.first_name = first_name;
                                                row.last_name = last_name;
                                                row.contact = contact;
                                                row.address = address;
                                                });
                        }
                        else {
                                //user is in table so modify
                                addresses.modify(iterator, user, [&]( auto& row ) {
                                                row.key = user;
                                                row.first_name = first_name;
                                                row.last_name = last_name;
                                                row.contact = contact;
                                                row.address = address;
                                                });
                        }
                }
        [[eosio::action]]
                void erase(name user) {
                        require_auth(user);
                        member_index addresses( get_self(), get_first_receiver().value);
                        auto iterator = addresses.find(user.value);
                        check(iterator != addresses.end(), "Record does not exist");
                        addresses.erase(iterator);
                }

        private:

                struct [[eosio::table]] memberinfo
                {
                        name key;
                        std::string first_name;
                        std::string last_name;
                        std::string contact;
                        std::string address;
                        uint64_t primary_key() const { return key.value;}
                };
                typedef eosio::multi_index<"people"_n, memberinfo> member_index;

};

Here is my abi:

{
    "____comment": "This file was generated with eosio-abigen. DO NOT EDIT ",
    "version": "eosio::abi/1.1",
    "types": [],
    "structs": [
        {
            "name": "erase",
            "base": "",
            "fields": [
                {
                    "name": "user",
                    "type": "name"
                }
            ]
        },
        {
            "name": "memberinfo",
            "base": "",
            "fields": [
                {
                    "name": "key",
                    "type": "name"
                },
                {
                    "name": "first_name",
                    "type": "string"
                },
                {
                    "name": "last_name",
                    "type": "string"
                },
                {
                    "name": "contact",
                    "type": "string"
                },
                {
                    "name": "address",
                    "type": "string"
                }
            ]
        },
         {
            "name": "upsert",
            "base": "",
            "fields": [
                {
                    "name": "user",
                    "type": "name"
                },
                {
                    "name": "first_name",
                    "type": "string"
                },
                {
                    "name": "last_name",
                    "type": "string"
                },
                {
                    "name": "contact",
                    "type": "string"
                },
                {
                    "name": "address",
                    "type": "string"
                }
            ]
        }
    ],
    "actions": [
        {
            "name": "erase",
            "type": "erase",
            "ricardian_contract": ""
        },
        {
            "name": "upsert",
            "type": "upsert",
            "ricardian_contract": ""
        }
    ],
    "tables": [
        {
            "name": "people",
            "type": "memberinfo",
            "index_type": "i64",
            "key_names": [],
            "key_types": []
        }
    ],
    "ricardian_clauses": [],
    "variants": []
}

Note: The account varshaccount is also used to deploy another smart contract before. So, I created new account that is memberacc, but facing same issue.

1
  • I created a new account with the same name of the contract. It's working fine for this.
    – Varsh
    Apr 13, 2020 at 2:01

1 Answer 1

2

You set get_first_receiver() as the scope of member_index. If this action is not sent by other contract, first_receiver will be the account where the contract is deployed.

[[eosio::action]]
void upsert(name user, std::string first_name, std::string last_name, std::string contact, std::string address) {
  require_auth( user );
  // 2nd argument here is the scope of member_index
  member_index addresses( get_self(), get_first_receiver().value );

cleos get table command accepts three fields, code, scope and table, so you should run cleos get table varshaccount varshaccount people.

1
  • Thank you for your help!
    – Varsh
    Apr 20, 2020 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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