Hi group, I am learning solidity, have a question. I know a bit what is doing this code in general but have line that I can't understand.
struct User {
string name;
string surName;
}
mapping (address => User) private users;


function getUser (address addr) returns(string, string) public view {
User memory user = users[addr];
return (user.name, user.surName);
}
The line is User memory user = users[addr];
user is User type (struct) so how it is comprehensible that I can pass an address key?

Apr 9, 2021, 2:21 AM
users is a mapping from an address to a User struct. addr is a key for accesing the value in that mapping. And when you retrieve a struct value from a mapping in a function, you would need to save it in memory.
The front-end develper I'm working with told me web3.js and metamask are not compatible, however, ethers.js works fine. I'm wondering if that is true. I can't believe web3. js can't work with metamask directly. Anyone knows?
Apr 9, 2021, 2:36 AM
Thanks, yes But the user is not mapping type, how can receive the key of mapping? That confuse me !
Apr 9, 2021, 2:38 AM
user is not a mapping, user is a struct object. users is a mapping (a bad naming practice that caused your confusion).
Apr 9, 2021, 2:39 AM
It is not injected, but I think you can still use it
Apr 9, 2021, 6:46 AM

© 2024 Draquery.com All rights reserved.