0
pragma solidity ^0.6.0;

contract UserCrud{
    struct userStruct{
        uint userAge;
        uint index;
        bytes32 userName;
        bytes32 userEmail;
    }
    mapping(address => userStruct) private userStructs;
    address[] private userIndex;

    function insertUser(
    address userAddress,
    uint userAge,
    bytes32 userName,
    bytes32 userEmail) public returns (uint index) {
        require(!isUser(userAddress));
        userStructs[userAddress].userAge = userAge;
        userStructs[userAddress].userName = userName;
        userStructs[userAddress].userEmail = userEmail;
        userStructs[userAddress].index = userIndex.push(userAddress) - 1; //error in this line
}

I got this code from an article regarding Solidity CRUD and was doing some modifications, but I'm facing an error as follows:

TypeError: Type tuple() is not implicitly convertible to expected type uint256. userStructs[userAddress].index = userIndex.push(userAddress) - 1;

1 Answer 1

1

The problems is that since solc v0.6.0 userIndex.push(userAddress) no longer return the length as in previous versions.

You can fix the code with something like this

userIndex.push(userAddress);
userStructs[userAddress].index = userIndex.length - 1;
2
  • It removed the error but gave a Warning: Unused function parameter. Remove or comment out the variable name to silence this warning. bytes32 userEmail) public returns (uint index) { which I can't remove. Sorry I'm new in solidity :(
    – Raman
    Oct 10, 2021 at 7:29
  • @Raman In this question ethereum.stackexchange.com/questions/72486/… you'll find a solution. In most cases it is faster to search the error message.
    – Ismael
    Oct 10, 2021 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.