Skip to content
Advertisement

Error: invalid string value (arg=”_name”, coderType=”string”, value=null)

I am trying to parse a persons information from a UI to a smart contract, the problem I seem to be having is the example I followed parses int’s and I’m not sure what to change in order to parse strings across? This code is just trying to get the player name and birthday.

Here is my smart contract code:

pragma solidity 0.6.6;

contract Athlete_contract4{
    string public playerName;
    string public playerBirthday;
    string public playerAddress;
    string public playerNationality;
    
   

 function setData(string memory _name, string memory _birthday) public{
        playerName = _name;
        playerBirthday = _birthday;
       // playerAddress = _address;
       // playerNationality = _nationality;
      
}
    
}

And my code for my UI:

<html>

<body>
    <div>
        <h4>Athlete Details</h4>
        <input type="text" id="name" placeholder="Name">
        <br><br>
        <input type="date" id="birthday">
        <br><br>
        <input type="text" id="address" placeholder="Address">
        <br><br>
        <input type="text" id="nationality" placeholder="Nationality">
        <br><br>
        
        <button id='submit'>Submit</button>
        
    </div>

    <script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js@1.0.0-beta.36/dist/web3.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" crossorigin="anonymous"></script>

    <script>
        var contract;

        $(document).ready(function () {
            web3 = new Web3(window.web3.currentProvider);

            var address = "0xD9190906543d08725f5d523A1CEd83Fcde4f1F28";
            var abi = [
    {
        "inputs": [],
        "name": "playerAddress",
        "outputs": [
            {
                "internalType": "string",
                "name": "",
                "type": "string"
            }
        ],
        "stateMutability": "view",
        "type": "function"
    },
    {
        "inputs": [],
        "name": "playerBirthday",
        "outputs": [
            {
                "internalType": "string",
                "name": "",
                "type": "string"
            }
        ],
        "stateMutability": "view",
        "type": "function"
    },
    {
        "inputs": [],
        "name": "playerName",
        "outputs": [
            {
                "internalType": "string",
                "name": "",
                "type": "string"
            }
        ],
        "stateMutability": "view",
        "type": "function"
    },
    {
        "inputs": [],
        "name": "playerNationality",
        "outputs": [
            {
                "internalType": "string",
                "name": "",
                "type": "string"
            }
        ],
        "stateMutability": "view",
        "type": "function"
    },
    {
        "inputs": [
            {
                "internalType": "string",
                "name": "_name",
                "type": "string"
            },
            {
                "internalType": "string",
                "name": "_birthday",
                "type": "string"
            }
        ],
        "name": "setData",
        "outputs": [],
        "stateMutability": "nonpayable",
        "type": "function"
    }
];


            contract = new web3.eth.Contract(abi, address);

            
        })

        $('#submit').click(function()
        {
          
            var name = 0;
            name = parseInt($('#name').val());
            
            var birthday = 0;
            birthday = parseInt($('#birthday').val());


            web3.eth.getAccounts().then(function(accounts){

                var acc = accounts[0];
                return contract.methods.setData(name, birthday).send({from: acc});
            }).then(function(tx)
            {
                console.log(tx);
            }).catch(function(tx)
            {
                console.log(tx);
            })
            
            

        });

        
    </script>

</body>

</html>

When I deploy this to a local host using http-server in visual studio code I get this error: enter image description here

Is there a parse string code that has to be used? Or have I just missed a line of code somewhere?

Advertisement

Answer

In your submit function – you are casting your “name” and “birthday” fields to an integer (where solidity is expecting a string).

Try removing the parseInt function.

name = parseInt($('#name').val()); 

to

name = $('#name').val();
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement