0

I have this code

struct User {
    string name;
    string state;
    uint age;
    string[] recommendations;
    address[] following;
}

mapping(address => User) public users;
mapping(address => bool) userExists;

...

function setup(string memory _name, uint _age, string memory _state) public {
    bytes memory nameBytes = bytes(_name);
    require(nameBytes.length > 0, "Name is empty.");
    users[msg.sender] = User(_name, _state, _age, users[msg.sender].recommendations, users[msg.sender].following);
    userExists[msg.sender] = true;
}
function hasAccount() public view returns (bool) {
    return userExists[msg.sender];
}

Which should just add a user to the users mapping and add true to the userExists mapping. This is reflected just fine in the unit test:

contract("Social Music", async accounts => {
const [initialAddress] = accounts;

let instance;

beforeEach(async () => {
    instance = await SocialMusic.deployed();
});

it('should set userExists after setup', async () => {
    await instance.setup('testname', 25, 'Test descr');

    const result = await instance.hasAccount();

    expect(result).to.be.true;
});
});

enter image description here

Which seems to be fine, but when I run the function setup in the browser, which shows up in ganache with correct params, nothing seems to be happening to the mapping afterwards.

Ganache: (FYI these are random strings)

enter image description here

and

enter image description here

Yet when calling hasAccount in the browser or looked at it in the ganache GUI it both seems like its not being added (browser gets false back when doing const hasAcc = await socialMusicInstance.methods.hasAccount().call();). I have no clue whatsoever what this can be about

4
  • No its not a bug in the UI, the browser also gives me back false so its reflected correctly Second doesnt answer my question
    – BrianM
    Apr 30 at 8:19
  • It is bug in ganache UI it will not show mappings although they worked correctly underneath. Until it is fixed don't trust ganache UI display for mappings. For the other question you have to pass the account to the view function call like this await socialMusicInstance.methods.hasAccount().call({from: "0x12341234..."}). To check if setup worked I'd suggest to emit an event with the data, they will appear in the transaction receipt if it was successful.
    – Ismael
    Apr 30 at 13:51
  • Indeed seems like it, my bad there
    – BrianM
    Apr 30 at 14:02
  • It's fine. Sometimes my English is not good enough. We are all learning new things! Keep up the good spirit!
    – Ismael
    Apr 30 at 15:05
0

So it seems that in the function hasAccount I am accessing msg.sender. It seems like this is not working in a view function, so it always returned false.

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.