Solidity Struct – Updating a Value Guide

blockchainsoliditystructweb3js

Hi i am trying to update a value in a struct and i feel like i have tried everything.

i think there is something important i do not know about using solidity structs.

my contracts is defined as:

pragma solidity ^0.4.17;

contract Profile {
    struct User {
        string displayName;
        string displayIcon;
    }

    mapping(address => User) public users;
    string name;

    function createUser() public {
        users[msg.sender] = User({
            displayName: "pre-set name",
            displayIcon: "pre-set icon"
        });
    }

    function getDisplayName() public view returns (string) {
        return users[msg.sender].displayName;
    }

    function getDisplayIcon() public view returns (string) {
        return users[msg.sender].displayIcon;
    }

    function setDisplayName(string newDisplayName) public {
        users[msg.sender].displayName = newDisplayName;
    }

    function setDisplayIcon(string newDisplayIcon) public {
        users[msg.sender].displayIcon = newDisplayIcon;
    }

    function getName() public view returns (string) {
        return name;
    }

    function setName(string newName) public {
        name = newName;
    }
}

i am calling the setters and getters with:

export const createUser = () => ({
    type: PROFILE_CREATE_NEW_USER,
    promise: ({ contracts, coinbase }) => contracts.Profile.createUser({ from: coinbase })
});

export const getDisplayName = () => ({
    type: PROFILE_GET_DISPLAY_NAME,
    promise: ({ contracts }) => contracts.Profile.getDisplayName()
});

export const setDisplayName = payload => ({
    type: PROFILE_SET_DISPLAY_NAME,
    promise: ({ contracts, coinbase }) => contracts.Profile.setDisplayName(payload, { from: coinbase })
});

export const getName = () => ({
    type: 'PROFILE_GET_NAME',
    promise: ({ contracts, coinbase }) => contracts.Profile.getName({ from: coinbase })
});

export const setName = newName => ({
    type: 'PROFILE_SET_NAME',
    promise: ({ contracts, coinbase }) => contracts.Profile.setName(newName, { from: coinbase })
});

but when i call these functions, it seems it seems name can be get get and set correctly, but the value of the struct property (displayName) is always an empty string if i try to get it.

i added the createUser method to see if that would fix the issue, but it still does not work.

i have tried this on Remix and it seems to work.

i'm new to solidity so i think i must be trying to debug this wrong. any help is appreciated.

thanks.

SOLVED

i have updated my code as suggested by a user below… and SUCCESS! the issue was you ALWAYS have to pass in { from: coinbase } when interacting with structs. the method getName() works with and without { from: coinbase }.

Best Answer

i have updated my code as suggested by a user below... and SUCCESS! the issue was you ALWAYS have to pass in { from: coinbase } when interacting with structs. the method getName() works with and without { from: coinbase }.

Related Topic