使用javascript将值返回到html时,我的实体图(int,string)出现函数错误。
我无法使用对Solidity智能合约函数的JavaScript函数调用来更新标签。我试图创建一个(整数,字符串)的实体映射,通过点击按钮在HTML中显示值。由于某种原因,threatActor.getActor(1,函数(错误,结果)返回NULL 有人能帮我看一下方向吗?谢谢 HTML代码:使用javascript将值返回到html时,我的实体图(int,string)出现函数错误。,javascript,html,solidity,Javascript,Html,Solidity,我无法使用对Solidity智能合约函数的JavaScript函数调用来更新标签。我试图创建一个(整数,字符串)的实体映射,通过点击按钮在HTML中显示值。由于某种原因,threatActor.getActor(1,函数(错误,结果)返回NULL 有人能帮我看一下方向吗?谢谢 HTML代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <met
<!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>
因此,当您说它返回NULL时,您的意思是回调函数中的
结果
为NULL,对吗?如果是这样,我有一些猜测,但没有具体内容:
映射可以做动态数组可以做的事情,32个字符(字节32
)对于大多数情况下使用字符串
已经足够了。我不确定这会在哪里引起问题,但我想我会指出这一点
是否可能在调用
addActor
后不久调用getActor
?如果是,则可能尚未挖掘addActor
的事务,因此尚未将参与者添加到合同中。通过初始化合同中的至少一个参与者,再试一次,看看会发生什么。
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;
}
}