1

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 }.

  • 2
    You are not passing { from: coinbase } when you call getDisplayName() – Ismael Mar 14 '18 at 20:26
  • i have tried with and without. i think you should be able perform read operations without having to pass in { from: coinbase }. you only need it for when performing write operations like setDisplayName('test', { from: coinbase }) – X0r0N Mar 14 '18 at 20:31
  • Since you use msg.sender in the getters it is better to specify from it will be used as msg.sender. Can you check if the transaction failed with out of gas? You can specify gas like this { from: "..", gas: "1000000" }, using strings can be expensive. – Ismael Mar 14 '18 at 20:41
  • i have updated my code as you suggested. and SUCCESS! thanks. so it you ALWAYS have to pass in { from: coinbase } when interacting with structs. the method getName() works with and without { from: coinbase }. thank you for your help. – X0r0N Mar 14 '18 at 20:46
  • You have to pass { from: "" } when you use msg.sender, getName() does work without because it doesn't use msg.sender. In any case it should be safe to always specify { from } with the current address. – Ismael Mar 14 '18 at 20:50
1

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 }.

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.