我正在开发一个以太坊dapp(投票)应用,我的智能合约中有一个函数来获取候选人列表,类型为bytes32[]。但是在 JavaScript 方面,我没有获取到值,只得到了 0x。如何解析这个值,以下是代码:
pragma solidity ^0.4.0;
contract Voting {
mapping (bytes32 => uint8) public votesReceived;
bytes32[] public candidateList;
string myString = "someString";
function Voting(bytes32[] candidateNames) public {
candidateList = candidateNames ;
}
function totalVotesFor(bytes32 candidate) view public returns (uint8) {
return votesReceived[candidate];
}
function addCandidate(bytes32 candidate) public returns (bool){
require(isNewEntry(candidate));
candidateList.push(candidate);
return isNewEntry(candidate);
}
function voteForCandidate(bytes32 candidate) public {
require(validCandidate(candidate));
votesReceived[candidate] += 1;
}
function getCandidateList() view public returns (bytes32[]) {
return candidateList;
}
function isNewEntry(bytes32 candidate) view public returns (bool) {
for(uint i = 0; i < candidateList.length; i++) {
if (candidateList[i] == candidate) {
return false;
}
}
return true;
}
function validCandidate(bytes32 candidate) view public returns (bool) {
for(uint i = 0; i < candidateList.length; i++) {
if (candidateList[i] == candidate) {
return true;
}
}
return false;
}
}
以下是访问合约函数的代码。
Voting.deployed().then(function(contractInstance) {
contractInstance.getCandidateList.call().then(function(v) {
console.log(v)
});
})
有人请帮帮我
(此段为已翻译内容)