运行此脚本时出错。无法调用voteForCandidate函数。它将引发错误无效响应和VM异常
var Web3 = require('web3')
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
abi = JSON.parse('[{"constant":true,"inputs":[{"name":"candidate","type":"bytes32"}],"name":"totalVotesFor","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"candidate","type":"bytes32"}],"name":"validCandidate","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"bytes32"}],"name":"votesReceived","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"candidateList","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"candidate","type":"bytes32"}],"name":"voteForCandidate","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"candidateNames","type":"bytes32[]"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]');
VotingContract = web3.eth.contract(abi);
// In your nodejs console, execute deployedContract.address to get the address at which the contract is deployed and change the line below to use your deployed address
contractInstance = VotingContract.at('0x0677792420741968f4595f466af369b308c25841');
candidates = {
"Narendra Modi": "candidate-1",
"Rahul Gandhi": "candidate-2",
"Soniya Gandhi": "candidate-3",
}
function voteForCandidate(id) {
var candidateName = $("input[name=votecandidate]").val();
// var candidateName = 'Narendra'
var voteflag = confirm("Confirm Vote");
if (voteflag == true) {
contractInstance.voteForCandidate(candidateName, {
from: web3.eth.accounts[0]
}, function () {
console.log(contractInstance.totalVotesFor.call(candidateName).toString());
window.location = "/voteadded/" + id;
});
}
}