Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Candidates are displayed on the screen in duplicate #2

Open
Krlucete opened this issue Mar 31, 2018 · 7 comments
Open

Candidates are displayed on the screen in duplicate #2

Krlucete opened this issue Mar 31, 2018 · 7 comments

Comments

@Krlucete
Copy link

2018-03-31 5 15 13

@asdimitrov13
Copy link

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.

@bardawilpeter
Copy link

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();
});
});
},

@popeyebot
Copy link

popeyebot commented May 10, 2018

Thanks peter0o, it is prefect !

@ghost
Copy link

ghost commented Jul 19, 2018

@bardawilpeter But if you change account and refresh again, the first refresh double render again.

@yodaster
Copy link

@bardawilpeter I agree with killermu94. As soon as you do the first refresh, it is still double rendered. The second refresh works correctly

@dwbelliston
Copy link

dwbelliston commented Sep 15, 2018

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) {
    ...

@moseley
Copy link

moseley commented May 17, 2019

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();
    })
},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants