0

我正在尝试将人员信息从 UI 解析为智能合约,我似乎遇到的问题是我遵循的示例解析 int,但我不确定要更改什么才能解析字符串?此代码只是试图获取玩家姓名和生日。

这是我的智能合约代码:

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;
      
}
    
}

我的 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>

http-server当我使用Visual Studio 代码 将其部署到本地主机时,出现此错误:在此处输入图像描述

是否有必须使用的解析字符串代码?还是我只是在某处遗漏了一行代码?

4

1 回答 1

2

在您的提交函数中-您将“姓名”和“生日”字段转换为整数(其中solidity 需要一个字符串)。

尝试删除 parseInt 函数。

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

name = $('#name').val();
于 2021-08-09T11:45:49.903 回答