我正在开发两个候选人之间的区块链投票应用程序。问题是,当我运行浏览器并将其连接到metamask时,包含候选人的表未显示在我的帐户上。浏览器没有监听或接收任何事件。我在Google Web控制台上进行了检查,发现使用web3作为后端的app.js文件中出现了一个错误。错误说:“未捕获(承诺)错误实例votedEvent不是函数”。谁能给我一个有关如何解决此错误的想法?我的应用程序运行良好,只是没有显示任何内容。任何建议都会有帮助
我对此问题进行了研究,发现该问题可能源于我与MetamasK的联系方式。我还听说我应该重新启动我的浏览器,这已经做了很多次,但是并不能解决问题。我已经与rinkleby,Ropsten和Kovan等不同的网络建立了联系。没用我什至创建了一个新的RPC网络,但无济于事。
App = {
web3Provider: null,
contracts: {},
account: '0x0',
hasVoted: false,
init: function() {
return App.initWeb3();
},
initWeb3: function() {
// TODO: refactor conditional
if (typeof web3 !== 'undefined') {
// If a web3 instance is already provided by Meta Mask.
App.web3Provider = web3.currentProvider;
web3 = new Web3(web3.currentProvider);
} else {
// Specify default instance if no web3 instance provided
App.web3Provider = new Web3.providers.HttpProvider('http://localhost:7545');
web3 = new Web3(App.web3Provider);
}
return App.initContract();
},
initContract: function() {
$.getJSON("Election.json", function(election) {
// Instantiate a new truffle contract from the artifact
App.contracts.Election = TruffleContract(election);
// Connect provider to interact with contract
App.contracts.Election.setProvider(App.web3Provider);
App.listenForEvents();
return App.render();
});
},
// Listen for events emitted from the contract
listenForEvents: function() {
App.contracts.Election.deployed().then(function(instance) {
// Restart Chrome if you are unable to receive this event
// This is a known issue with Metamask
// https://github.com/MetaMask/metamask-extension/issues/2393
instance.votedEvent({}, {
fromBlock: 0,
toBlock: 'latest'
}).watch(function(error, event) {
console.log("event triggered", event)
// Reload when a new vote is recorded
App.render();
});
});
},
它运行了,但是没有产生任何结果。我希望它能以表格形式显示参加比赛的候选人的姓名和投票