Comments (7)
I also had this issue, another fix is to handle the promises differently. Group all the promise calls to get candidates to an array, then waiting until all of them are resolved to add them to the dom.
...
// Load contract data
App.contracts.Election.deployed().then(function (instance) {
electionInstance = instance;
return electionInstance.candidatesCount();
}).then(function (candidatesCount) {
// Store all promised to get candidate info
const promises = [];
for (var i = 1; i <= candidatesCount; i++) {
promises.push(electionInstance.candidates(i));
}
// Once all candidates are received, add to dom
Promise.all(promises).then((candidates) => {
var candidatesResults = $("#candidatesResults");
candidatesResults.empty();
var candidatesSelect = $('#candidatesSelect');
candidatesSelect.empty();
candidates.forEach(candidate => {
var id = candidate[0];
var name = candidate[1];
var voteCount = candidate[2];
// Render candidate Result
var candidateTemplate = "<tr><th>" + id + "</th><td>" + name + "</td><td>" + voteCount + "</td></tr>"
candidatesResults.append(candidateTemplate);
// Render candidate ballot option
var candidateOption = "<option value='" + id + "' >" + name + "</ option>"
candidatesSelect.append(candidateOption);
})
});
return electionInstance.voters(App.account);
}).then(function (hasVoted) {
...
from election.
You can just update the listenEvent to the below and it will work:
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
// MetaMask/metamask-extension#2393
instance.votedEvent({}, {
fromBlock: 'latest',
toBlock: 'latest'
}).watch(function(error, event) {
console.log("event triggered", event)
// Reload when a new vote is recorded
App.render();
});
});
},
from election.
I have noticed the same thing, I think it's got something to do with the async calls from the promises, however, I couldn't identify the root of the problem myself.
from election.
Thanks peter0o, it is prefect !
from election.
@bardawilpeter But if you change account and refresh again, the first refresh double render again.
from election.
@bardawilpeter I agree with killermu94. As soon as you do the first refresh, it is still double rendered. The second refresh works correctly
from election.
I added a watch event for account change. Added App.listenForAccountChange() in the initContract function.
listenForAccountChange: function() {
ethereum.on('accountsChanged', function (accounts) {
App.account = accounts[0];
App.render();
})
},
from election.
Related Issues (20)
- Under truffle development console on typing "candidate" it's coming out :Uncaught reference error HOT 1
- facing issue while running with metamask HOT 3
- facing issue while connecting to MetaMask HOT 7
- Truffle Test ReferenceError: accounts not defined HOT 1
- truffle compile error HOT 1
- Issue in truffle migrate
- issue in front-end HOT 1
- facing this Issue while running command "truffle test".
- issues with meta mask HOT 3
- Candidates not showing in my dropdown menu HOT 1
- Uncaught promise in "app.js" & no table visible on browser. HOT 4
- TypeError: Indexed expression has to be a type, mapping or array (is uint256)
- web3.eth.accounts[0] returns undefined and app.vote(1,{ from:web3.eth.accounts[0] }) is giving error HOT 2
- throws an exception for double voting: AssertionError: accepts first vote: expected <BN: 0> to equal 1
- throws an exception for double voting. AssertionError: accepts first vote: expected <BN: 0> to equal 1 HOT 8
- If you are stuck with various problems, check this 2022 version out-of-the-box fork HOT 7
- Trying to convert the callback tests to async
- for whatever reason, it doesn't throw exception. i fixed the test to check for failed tx status instead: https://github.com/pauluhn/election/blob/paul/test/election.js#L54
- Candidates are repeated after casting votes from different account HOT 1
- Create app
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from election.