[英]Firebase ensuring a record is not repeated
I have this code:我有这个代码:
public static void insertvote(String userkey, String categ, String candId) {
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference totalVotesRef = rootRef.child("votes").child(categ).child(candId);
Vote vote = new Vote(userkey);
totalVotesRef.push().setValue(vote.getVoterEmail());
}
It is supposed to insert votes into a firebase database, in a votes collections as below:它应该将投票插入到 firebase 数据库中,在如下的投票集合中:
The idea is to ensure a voter only votes once due to integrity by checking if their email exists in the votes.
这个想法是通过检查他们的电子邮件是否存在于投票中来确保选民由于完整性而只投票一次。 However, as you can see, a voter can vote twice.
但是,如您所见,选民可以投票两次。 Is there a way I can make a function that checks whether the email exixts in the database, and if it does exist, tell the voter they cant vote twice?
有没有办法可以创建一个函数来检查数据库中的电子邮件是否存在,如果存在,告诉选民他们不能投票两次? Thanks in advance.
提前致谢。
You should use the Firebase Auth UID of the current user as the name of the child key.您应该使用当前用户的 Firebase Auth UID 作为子密钥的名称。
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
totalVotesRef.child(uid).setValue(vote.getVoterEmail());
I will give you an idea, think about it.我给你一个想法,你考虑一下。 If you use Shared Preferences, you can put every e-mail address with a boolean flag(True/False - Voted/not).
如果您使用共享首选项,您可以将每个电子邮件地址与布尔标志(真/假 - 投票/否)。 Then, you can call the Shared Preferences and check the boolean value of the flag for every e-mail address.
然后,您可以调用共享首选项并检查每个电子邮件地址的标志的布尔值。 So, if it's true - don't allow him to vote.
所以,如果这是真的——不要让他投票。
Wish I helped :)希望我有所帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.