我试图将html下拉列表中的值发送到智能契约,该值是从js函数中提取出来的,但我无法将其添加到SetVote函数中,执行就会中断。
Html:
Dropdown button
value1
value2
value3
JavaScript:
App = { web3Provider: null, contracts: {},
init: function() {
console.log("inside init");
return App.initWeb3(); },
initWeb3: function() {
console.log("inside initWeb3");
// Is there an injected web3 instance? if (typeof web3 !== 'undefined') { App.web3Provider = web3.currentProvider; } else { // If no injected web3 instance is detected, fall back to Ganache App.web3Provider = new Web3.providers.HttpProvider('http://127.0.0.1:9545'); } web3 = new Web3(App.web3Provider);
return App.initContract(); },
initContract: function() {
console.log("inside initContract");
$.getJSON('voting.json', function(data) {
// Get the necessary contract artifact file and instantiate it with truffle-contract
var votingArtifact = data;
App.contracts.voting = TruffleContract(votingArtifact);
// Set the provider for our contract
App.contracts.voting.setProvider(App.web3Provider);
// Use our contract to retrieve and mark the adopted pets
// return App.markAdopted();
});
return App.bindEvents(); },
bindEvents: function() {
$(document).on('click', '.btn-res', App.GetVote);
//$(document).on('click', '.btn-vote', App.SetVote);
$(".dropdown-menu a").click(function(){var vote = $(this).text(); });
console.log("voted value",vote);
App.SetVote(vote);
},
SetVote: function(account) {
var votingInstance;
//var vote="voteValue"
App.contracts.voting.deployed().then(function(instance) {
votingInstance = instance;
return votingInstance.setVote(vote); }).then(function() {
// modal for successfully voting
// open index page
}).catch(function(e) {
console.log("error in voting"); });
},
GetVote: function() {
web3.eth.getAccounts(function(error, accounts) {
if (error) {
console.log(error);
}
var account = accounts[0];
var self = this;
var data;
voting.deployed().then(function(instance) {
data = instance;
return data.getVote.call();
}).then(function(value) {
console.log(value);
//table of winners
//list of voters
}).catch(function(e) {
console.log(e);
self.setStatus("Error getting balance; see log.");
}); }); } }; $(function() { $(window).load(function() {
App.init(); }); });发布于 2018-06-18 05:12:44
在我看来,setVote param帐户没有发送到contract。请更正变量,并查看是否正在调用契约方法。
https://ethereum.stackexchange.com/questions/51455
复制相似问题