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;
});
});
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)
and
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
false
so its reflected correctly Second doesnt answer my questionawait 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.