0

As you can see, I have designed a struct in the solidity.There is a function in the solidity with the return value type User

mapping(address => User) public _userInfoMap;
struct User{
        uint value;
        string email;
        string phoneNumber;
        string name;
        string idCard;
        Gender gender;
        int256 birthday;
        string province;
        string city;
        string area;
    }

function getUserInfoByAccount(address account) public view returns (User memory) {
    return _userInfoMap[account];
}

I use the web3js to call the function and get the return value:

let res = await this.userContract.methods.getUserInfoByAccount(account).call({from: this.admin});

And when I print the res, I find all the int type has been changed to string. The printed result is below.

area: "0xe984a299b5e58ebf"
birthday: "8161300000"
city: "0xe88e6988ce5b882"
email: "927694@qq.com"
gender: "0"
idCard: "41133428"
name: "0xe78e8be5a49ae9b1bc"
phoneNumber: "1871778"
province: "0xe6b3e58d97e79c81"
value: "0"

I don't know why all int variables become strings. I want to know if this is normal? Hope you can give me some help!

  • "I don't know why all int variables become strings" - Because the returned integer value can be larger than Number.MAX_SAFE_INTEGER (2 ^ 53 - 1). You can use web3.utils.toBN in order to convert it from a string to a BN object, which will allow you to perform arithmetic calculations on it via class BN functions (add, mul, sub, div, etc). – goodvibration Jan 8 at 9:42
  • Thank you for your reply! Can I understand it this way, it is javascript that helped me turn int into string to prevent overflow – wei wang Jan 8 at 9:58
  • Web3.js, to be more accurate. – goodvibration Jan 8 at 11:34
  • Thank you for your reply! – wei wang Jan 8 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.