使用Java脚本将值返回到html时,我的实度映射(int,字符串)出现函数错误。

Function error with my Solidity map(int,string) while using javascript to return values to html.(使用Java脚本将值返回到html时,我的实度映射(int,字符串)出现函数错误。)

本文介绍了使用Java脚本将值返回到html时,我的实度映射(int,字符串)出现函数错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法通过调用Solidity智能合同函数的JavaScript函数来更新我的标签。我正在尝试创建一个(整型,字符串)立体映射,只需点击一下按钮就可以在HTML中显示值。由于某种原因,威胁Actor.getActor(1,Function(Error,Result)返回空。

有谁能帮我看看正确的方向吗?谢谢。

HTML代码:

 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>

    <link rel="stylesheet" type="text/css" href="main.css">

    <script src="/home/zachary/employee-eth/node_modules/web3/dist/web3.min.js"></script>


</head>
<body>
    <div class="container">
    <div class="container1">

        <label for="labels" class="col-lg-2 control-label">Labels: </label>
        <input id="labels" type="text">

        <label for="aliases" class="col-lg-2 control-label">Aliases: </label>
        <input id="aliases" type="text">

        <label for="roles" class="col-lg-2 control-label">Roles: </label>
        <input id="roles" type="text">

        <label for="goals" class="col-lg-2 control-label">Goals: </label>
        <input id="goals" type="text">

        <label for="resource_level" class="col-lg-2 control-label">Organization: </label>
        <input id="resource_level" type="text">

        <label for="sophistication" class="col-lg-2 control-label">Sophistication: </label>
        <input id="sophistication" type="text">

        <label for="primary_motivation" class="col-lg-2 control-label">Primary Motivation: </label>
        <input id="primary_motivation" type="text">

        <button id="buttonSet">Submit Threat</button>
        <div id="container">
        <div id="container1"><button id="buttonGet">Get Threats by ID</button></div>
        <div id="container2"><input id="getId" type="text" value="1"></div>
        </div>

    </div>
    <div class="container2">

        <h1>Actor Attributes</h1>

        <label for="actorID" class="col-lg-2 control-label">ID: </label>
        <label id="actorID_d" class="col-lg-2 control-label">Empty</label>

        <label for="labels" class="col-lg-2 control-label">Labels: </label>
        <label id="labels_d" class="col-lg-2 control-label">Empty</label>

        <label for="aliases" class="col-lg-2 control-label">Aliases: </label>
        <label id="aliases_d" class="col-lg-2 control-label">Empty</label>

        <label for="roles" class="col-lg-2 control-label">Roles: </label>
        <label id="roles_d" class="col-lg-2 control-label">Empty</label>

        <label for="goals" class="col-lg-2 control-label">Goals: </label>
        <label id="goals_d" class="col-lg-2 control-label">Empty</label>

        <label for="resource_level" class="col-lg-2 control-label">Organization: </label>
        <label id="resource_level_d" class="col-lg-2 control-label">Empty</label>

        <label for="sophistication" class="col-lg-2 control-label">Sophistication: </label>
        <label id="sophistication_d" class="col-lg-2 control-label">Empty</label>

        <label for="primary_motivation" class="col-lg-2 control-label">Primary Motivation: </label>
        <label id="primary_motivation_d" class="col-lg-2 control-label">Empty</label>

    </div>
</div>
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>

    <script>

        if (typeof web3 !== 'undefined') {
            web3 = new Web3(web3.currentProvider);
        } else {
            web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
        }
        web3.eth.defaultAccount = web3.eth.accounts[0];
        var threatActorContract = web3.eth.contract([
    {
        "constant": false,
        "inputs": [
            {
                "name": "_labels",
                "type": "string"
            },
            {
                "name": "_aliases",
                "type": "string"
            },
            {
                "name": "_roles",
                "type": "string"
            },
            {
                "name": "_goals",
                "type": "string"
            },
            {
                "name": "_resource_level",
                "type": "string"
            },
            {
                "name": "_sophistication",
                "type": "string"
            },
            {
                "name": "_primary_motivation",
                "type": "string"
            }
        ],
        "name": "addActor",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [
            {
                "name": "",
                "type": "uint256"
            }
        ],
        "name": "actors",
        "outputs": [
            {
                "name": "labels",
                "type": "string"
            },
            {
                "name": "aliases",
                "type": "string"
            },
            {
                "name": "roles",
                "type": "string"
            },
            {
                "name": "goals",
                "type": "string"
            },
            {
                "name": "resource_level",
                "type": "string"
            },
            {
                "name": "sophistication",
                "type": "string"
            },
            {
                "name": "primary_motivation",
                "type": "string"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "countActors",
        "outputs": [
            {
                "name": "",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [
            {
                "name": "_key",
                "type": "uint256"
            }
        ],
        "name": "getActor",
        "outputs": [
            {
                "name": "",
                "type": "string"
            },
            {
                "name": "",
                "type": "string"
            },
            {
                "name": "",
                "type": "string"
            },
            {
                "name": "",
                "type": "string"
            },
            {
                "name": "",
                "type": "string"
            },
            {
                "name": "",
                "type": "string"
            },
            {
                "name": "",
                "type": "string"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    }
]);

        var threatActor = threatActorContract.at("0xcf5f9fb59f9b22810c50a9158b393388c492c258");
        console.log(threatActor);

        $("#buttonSet").click(function() {
            threatActor.addActor($("#labels").val(), $("#aliases").val(), $("#roles").val(), $("#goals").val(),
            $("#resource_level").val(), $("#sophistication").val(), $("#primary_motivation").val());
        });

        $("#buttonGet").click(function() {

            threatActor.getActor(1,function(error, result) {

            if(!error)
                {
                    document.getElementById("#labels_d").innerHTML = result[0];
                    document.getElementById("#aliases_d").innerHTML = result[1];
                    document.getElementById("#roles_d").innerHTML = result[2];
                    document.getElementById("#goals_d").innerHTML = result[3];
                    document.getElementById("#resource_level_d").innerHTML = result[4];
                    document.getElementById("#sophistication_d").innerHTML = result[5];
                    document.getElementById("#primary_motivation_d").innerHTML = result[6];
                    console.log(result);
                }
                else {
                    console.error("ERROR!");
                }
        });

        });
    </script>

</body>
</html>

稳固智能合同代码:

 pragma solidity ^0.4.24;
    contract ThreatActors {

        uint actorAmount = 1;

        struct Actor{
            string labels;
            string aliases;
            string roles;
            string goals;
            string resource_level;
            string sophistication;
            string primary_motivation;
        }

       Actor[] public actors;


    function addActor(string _labels, string _aliases, string _roles, string _goals, string _resource_level, string _sophistication, string _primary_motivation) public
        {
            Actor memory actor = Actor(_labels, _aliases, _roles, _goals, _resource_level, _sophistication, _primary_motivation);

            actors.push(actor);

            actorAmount++;
        }


    function getActor(uint _key) view public returns (string, string, string, string, string, string, string) {
        return (actors[_key].labels, actors[_key].aliases, actors[_key].roles, actors[_key].goals, actors[_key].resource_level, actors[_key].sophistication, actors[_key].primary_motivation);
    }

    function countActors() view public returns (uint) {
        return actors.length;
    }

    }

推荐答案

所以当您说它返回NULL时,您的意思是回调函数中的result是NULL,对吗?如果是这样的话,我有几个猜测,但没有具体的:

  1. 您的输入在传递给函数时不知何故被转换为错误的。它需要的是uint256,而您给的是数字文字1。我不得不假设这应该可以工作,但我通常坚持用BigNumber来包装我的数字,从来没有使用过文字(但再说一次,它为什么不能工作)。
  2. 您正在实心中使用字符串。为了绝对控制我的结构大小和内存使用,我几乎从不使用动态数组。它们当然有用处,但通常mapping可以做动态数组可以做的事情,对于大多数情况下使用string来说,32个字符(bytes32)就足够了。我不确定这可能会在哪里导致问题,但我想我会指出它。
  3. 可能您的Web3版本是错误的。我知道有些版本可以简单地调用函数,而另一些版本则更喜欢使用Call/Send(取决于它是否是常量函数)。如果您使用的是1.0,情况可能是seen in the docs。

这篇关于使用Java脚本将值返回到html时,我的实度映射(int,字符串)出现函数错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用Java脚本将值返回到html时,我的实度映射(int,字符串)出现函数错误。

基础教程推荐