简体   繁体   English

如何防止双重投票反应-Firebase

[英]how to prevent double upvote react- firebase

I'm trying to prevent double voting in firebase. 我正在尝试阻止Firebase中的双重投票。 My idea was to make a vote object in firebase and then add a user_id boolean every time someone votes. 我的想法是在firebase中创建一个表决对象,然后在有人投票时添加一个user_id布尔值。 So counterTips>votes>User_id1: true> UserId2: True. 所以counterTips>votes>User_id1: true> UserId2: True.

These were my security rules : 这些是我的安全规则:

{

  "rules": {
    ".read": true,
    ".write": true,
      "CounterTips": {
     "votes": {
                "$uid": {
                    ".write": "auth.uid != null && auth.uid === $uid"
                }
              }
            }
  }


}

This is the way my db is structured 这是我的数据库的结构方式

CounterTips 

 user_id: "rI9m9GEkyKhopAhaDNC8GNWPS943"

 vote: 5, 

 votes: 
1) bbz9MLo1u7T7zxugmHA806nMof93: true

I'm trying to push current users id into votes object, but instead am just getting a static string that says user_id: true. 我试图将当前的用户ID推入投票对象,但只是获得一个静态字符串,表示user_id:true。 How do I push an actual user_id to votes and prevent that user from voting again in the security rules? 如何在安全性规则中将实际的user_id投票,并阻止该用户再次投票?

     upvotePost(key, vote, user_id) {

if (this.state.user_id !=null )
{

CounterTipsref.child("votes/${this.state.user_id}").once("value",snapshot => {
    if (snapshot.exists()){
      const userData = snapshot.val();
      console.log("exists!", userData);
    }


});
}

 else{
alert(this.state.user_id);
let user_id= this.state.user_id;
let votes = { [user_id]: true};

vote++;
CounterTipsRef.child(key).update({ 'vote': vote, 'votes': votes }); 
}
}
if (this.state.user_id==null) {

this.props.history.push('/login');
}


  }
}

Your problem is that you're using this notation: 您的问题是您正在使用以下表示法:

let votes = { user_id: true};

The user_id in here is seen as the name of the property, instead of the variable holding the name of the property. user_id在这里被看作是属性的名称,而不是变量持有物业的名称。 The quick fix is to use [] notation: 快速的解决方法是使用[]表示法:

let votes = { };
votes[user_id] = true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM