I've been stuck for 2 months

Guys, since I finished my Frontend certification in January and decided to skip the data viz cert and go to the back-end cert, I’ve been stuck. I’ve been unable to complete the voting app.
How do I track voters in the db?

Could you link us to some code so I can see how far you got? That way we can try work through it :slight_smile:

1 Like

If you are using Mongodb create a sperate collection for questions(as objects). Then make an array of choices for each question and each choice is an object with choice name(string) and votes(array of user ids.)

const QuestionSchema = new mongoose.schema({
    content: String,
    owner: String,
    choices: [{
         name: String,
         votes: Array
    }]
});

or you can make seperate array of voters on question object and increase votes count for choice at the time of vote, check if the user id is not an element of voters array.

const QuestionSchema = new mongoose.schema({
    content: String,
    owner: String,
    choices: [{name: String, votes: Number}],
    voters: [String] //user ids into voters
});
1 Like

Were you able to complete the API Projects for the Backend Certification?
I found the Clementine.JS tutorial to be pretty helpful in regards to learning how to do a fullstack app. ClementineJS is mentioned in the Get Set for our Dynamic Web Application Projects step in the map. I would work through that if you haven’t already.
I am using Angular.JS instead of just regular JS to keep data on the front-end and back-end in synch, but the Clementine.JS tutorial walks you through doing it with just AJAX if you haven’t learned any frameworks yet. Now might be a good time to try one!
I am on the Voting App too. I am not “stuck” but do consider it much harder than the API Projects.

You could check out my project code on my github. I do know what’s it’s like when working on the same project for a month (or even 2 months), things can get frustrating, and you need to vent / distract yourself / recover from burnout.

I think modularisation is the key.

I’d say step 1 is identifying how you are going to solve a problem, and matching with tools/libaries you have found which can help.

  • separating transport websockets from logic
  • user authentication (user is who they say they), express middleware
  • user authorisation
    1. user has permission to only vote once per post
    2. user who is owner has permission to remove post

and step 2 which i find the more annoying part is continuously implementing this logic and data into code, that hopefully is structured enough for another developer to read and have his witts.

(tbh step 1 can also be as hard if not harder than step 2, step 3 is deploying it then discovering bugs…)

practically you could

var o = {} // exports for another module
const mongo = require('mongodb')

mongo.connect('localhost:27017/votes').then(function(db){
  o.db = db
  o.connected = true
})

// defaultPoll not used, example of input to putPoll
var defaultPoll = {
  // _id: mongo generates this on insert
  user: 'harry',
  question: 'Favourite hair style?',
  answers: [ 'mohawk', 'gradient shaved', 'short' ]
}

function putPoll( poll ) {
  if (o.connected !== true) {return}
  return db.collection('polls').insert(polss)
}

var defaultVote = {
  user: 'dave',
  poll_id: '...'// link to poll
  answer: 'mohawk'
}
function putVote( vote ) {
  if (o.connected !== true) {return}
  return db.collection('votes').insert(vote)
}

function getPollWithVotes(poll_id) {
  var output = {}
  return db.collection('polls').findOne({_id: mongo.ObjectId(poll_id)})
  .then(function(poll){
    output.poll = poll
  })
  .then(function(){
    return db.collection('votes').find({poll_id: poll_id}).toArray()
  })
  .then(function(votes){
    output.votes = votes
    return output
  })
}
o.methods = {
  putPoll,
  putVote,
  getPollWithVotes
}
module.exports = o // for use in express, websockets

obviously putting and organising the framework around things is harder than simple db operations. that and designing the frontend

have you done any mongo tutorials for example the learnyoumongo tutorial?

You’ll need to share code and what you’ve tried in order for us to help you. LIve help often available in the gitter backend room as well.