0

Hey so this is my solidity code, when I am running the getter functions, I am not seeing any output. And as for a fact I don't even know whether the setter functions are even working or not. Please help me out folks.

pragma solidity ^0.8.7;

contract data_manipulation {

struct User {
    string first_name;
    string last_name;
    uint256 phone_number;
    string email_address;
    string delivery_address;
}

struct Seller {
    string first_name;
    string last_name;
    uint256 phone_number;
    string delivery_address;
}

mapping(address => User) public users;
mapping(address => Seller) public sellers;

function AddUser(string memory first_name, string memory last_name, uint256 phone_number, string memory email_address, string memory delivery_address) public view{
    User memory user_instance = users[msg.sender];
    user_instance.first_name = first_name;
    user_instance.last_name = last_name;
    user_instance.phone_number = phone_number;
    user_instance.email_address = email_address;
    user_instance.delivery_address = delivery_address;

}

// Setter Functions

function set_first_name(string memory first_name_setter) public view{
    User memory user_instance = users[msg.sender];
    user_instance.first_name = first_name_setter;

}

function set_last_name(string memory last_name_setter) public view{
    User memory user_instance = users[msg.sender];
    user_instance.last_name = last_name_setter;

}

function set_phone_number(uint256 phone_number_setter) public view{
    User memory user_instance = users[msg.sender];
    user_instance.phone_number = phone_number_setter;

}

function set_email_address(string memory email_address_setter) public view{
    User memory user_instance = users[msg.sender];
    user_instance.email_address = email_address_setter;

}    

function set_delivery_address(string memory delivery_address_setter) public view{
    User memory user_instance = users[msg.sender];
    user_instance.delivery_address = delivery_address_setter;

}

// Getter Functions

function get_first_name() public view returns(string memory){
    User memory user_instance = users[msg.sender];
    return user_instance.first_name;

}

function get_last_name() public view returns(string memory){
    User memory user_instance = users[msg.sender];
    return user_instance.last_name;

}

function get_phone_number() public view returns(uint256){
    User memory user_instance = users[msg.sender];
    return user_instance.phone_number;

}

function get_email_address() public view returns(string memory){
    User memory user_instance = users[msg.sender];
    return user_instance.email_address;

}    

function get_delivery_address() public view returns(string memory){
    User memory user_instance = users[msg.sender];
    return user_instance.delivery_address;

}

1 Answer 1

0

In your code the problem is that you're defining the attribute of User's struct but you cannot set User's memory instance into a users mapping. For only setter function you must only add after latest instruction this statement:

users[msg.sender] = user_instance;

This mean that user_instance memory struct is inserted into users mapping into value of msg.sender key. I updated your code in this way:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

contract data_manipulation {

    struct User {
        string first_name;
        string last_name;
        uint256 phone_number;
        string email_address;
        string delivery_address;
    }

    struct Seller {
        string first_name;
        string last_name;
        uint256 phone_number;
        string delivery_address;
    }

    mapping(address => User) public users;
    mapping(address => Seller) public sellers;

    function AddUser(string memory first_name, string memory last_name, uint256 phone_number, string memory email_address, string memory delivery_address) public view{
        User memory user_instance = users[msg.sender];
        user_instance.first_name = first_name;
        user_instance.last_name = last_name;
        user_instance.phone_number = phone_number;
        user_instance.email_address = email_address;
        user_instance.delivery_address = delivery_address;

    }

    // Setter Functions

    function set_first_name(string memory first_name_setter) public {
        User memory user_instance = users[msg.sender];
        user_instance.first_name = first_name_setter;
        users[msg.sender] = user_instance;
    }

    function set_last_name(string memory last_name_setter) public {
        User memory user_instance = users[msg.sender];
        user_instance.last_name = last_name_setter;
        users[msg.sender] = user_instance;
    }

    function set_phone_number(uint256 phone_number_setter) public {
        User memory user_instance = users[msg.sender];
        user_instance.phone_number = phone_number_setter;
        users[msg.sender] = user_instance;
    }

    function set_email_address(string memory email_address_setter) public {
        User memory user_instance = users[msg.sender];
        user_instance.email_address = email_address_setter;
        users[msg.sender] = user_instance;
    }    

    function set_delivery_address(string memory delivery_address_setter) public {
        User memory user_instance = users[msg.sender];
        user_instance.delivery_address = delivery_address_setter;
        users[msg.sender] = user_instance;
    }

    // Getter Functions

    function get_first_name() public view returns(string memory){
        User memory user_instance = users[msg.sender];
        return user_instance.first_name;

    }

    function get_last_name() public view returns(string memory){
        User memory user_instance = users[msg.sender];
        return user_instance.last_name;

    }

    function get_phone_number() public view returns(uint256){
        User memory user_instance = users[msg.sender];
        return user_instance.phone_number;

    }

    function get_email_address() public view returns(string memory){
        User memory user_instance = users[msg.sender];
        return user_instance.email_address;

    }    

    function get_delivery_address() public view returns(string memory){
        User memory user_instance = users[msg.sender];
        return user_instance.delivery_address;

    }
} 

NOTE: I removed the view modifier because you're modifying the state of variables.

1
  • I switched the places where I have to store permanent data, to storage from memory, it works for me that way tho. Please let me know if this will give rise to any other issues in the future. Commented Apr 9, 2022 at 15:51

Your Answer

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

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