question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

voting does not reflect changes on result app.

See original GitHub issue

cloned the repo, ran docker-compose up.

on voting, the logs gives

vote_1    | 10.0.19.2 - - [22/Dec/2016 20:04:10] "POST / HTTP/1.1" 200 -
worker_1  | Processing vote for 'a' by 'cceb022e236a011b'
db        | ERROR:  duplicate key value violates unique constraint "votes_id_key"
db        | DETAIL:  Key (id)=(cceb022e236a011b) already exists.
db        | STATEMENT:  INSERT INTO votes (id, vote) VALUES ($1, $2)

also it does not reflect any change on the result app , even when voting for the first time.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
sparacommented, Jan 20, 2017

The duplicate key error occurs when you try to vote more than once from the same client. Each vote is inserted into the database using a cookie generated by the vote application. When you try to vote from the same client it will use the same id from the cookie which violates the duplicate key constraint in the database. Will not fix.

1reaction
thanley11commented, Dec 27, 2016

I upgraded to postgres 9.6 and am now using the upsert functionality.

INSERT INTO votes (id, vote) VALUES (@id, @vote) ON CONFLICT (id) DO UPDATE SET vote = @vote.  

I think that may not be the issue, however, as the exception should be caught in the code and the update should happen. One issue I found is that the results app is not really connecting to postgres because of this in result/server.js:

var config = {
   host: 'db', // Need to change to name of alias in docker-compose ('db'), instead of host: 'localhost',
   user: 'postgres',
   password: '',
   database: 'postgres',
   ...
   });
 });

I changed this and also modified the getVotes function and it is all working:

 function getVotes(client) {
  client.query('SELECT vote, COUNT(id) AS count FROM votes GROUP BY vote ORDER BY vote ASC', [], function(err, result) {
     if (err) {
       console.error("Error performing query: " + err);
     } else {
        var votes = result.rowCount;
        if (result.rowCount < 2){
            votes = [result.rows[0], {"count" : 0} ];
        }
        else {
            votes = [result.rows[0], result.rows[1]];
        }
       io.sockets.emit("scores", JSON.stringify(votes));
     }
     setTimeout(function() {getVotes(client) }, 1000);
});
}

And in result/views/app/js

  var updateScores = function(){
    socket.on('scores', function (json) {
       data = JSON.parse(json);
       var a = parseInt(data[0].count || 0);
       var b = parseInt(data[1].count || 0);

       var percentages = getPercentages(a, b);

       bg1.style.width = percentages.a + "%";
       bg2.style.width = percentages.b + "%";

       $scope.$apply(function () {
         $scope.aPercent = percentages.a;
         $scope.bPercent = percentages.b;
         $scope.total = a + b;
       });
    });
  };

Read more comments on GitHub >

github_iconTop Results From Across the Web

What 2020's Election Poll Errors Tell Us About the ...
Most preelection polls in 2020 overstated Joe Biden's lead over Donald Trump in the national vote for president, and in some states ...
Read more >
Frequently Asked Questions - California Secretary of State
Voter Registration. Who can register to vote? To register to vote in California, you must be: A United States citizen and a resident ......
Read more >
All Frequently Asked Questions for Elections | Tennessee ...
Frequently Asked Questions for this Division. Absentee Voting. Can I hand deliver my ballot to the election office?
Read more >
Title 52- Voting and Elections- Subtitle I and II
(1) All citizens of the United States who are otherwise qualified by law to vote at any election by the people in any...
Read more >
How to register to vote | USAGov
Use vote.gov to register to vote in federal, state, and local elections. If you're a U.S. citizen living abroad, see how you can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found