Hey there! I'm currently working on a voting system and I want to prevent the same user from voting multiple times on the same post.
let db = firebase.firestore();
var postRef = db.collection("posts").doc(this.pid);
postRef.update({
votes: firebase.firestore.FieldValue.increment(1)
});
var userRef = db.collection("users").doc(this.userId);
userRef.update({
votes: firebase.firestore.FieldValue.arrayUnion(this.pid)
});
//execute this line if pid is added
this.votes = this.votes + 1;
I only want to increase the vote count if the post id (pid) is successfully added to the votes array. I'm curious if the arrayUnion method provides any feedback on this or if there's another way to achieve it.
You can check out this post and see that the same individual can cast multiple votes on the same post.