我正在尝试防止 firebase 中的双重投票。我的想法是在 firebase 中创建一个投票对象,然后在每次有人投票时添加一个 user_id bool 值。所以 counterTips>votes>User_id1: true> UserId2: True。
这些是我的安全规则:
{
"rules": {
".read": true,
".write": true,
"CounterTips": {
"votes": {
"$uid": {
".write": "auth.uid != null && auth.uid === $uid"
}
}
}
}
}
这就是我的数据库的结构方式
CounterTips
user_id: "rI9m9GEkyKhopAhaDNC8GNWPS943"
vote: 5,
votes:
1) bbz9MLo1u7T7zxugmHA806nMof93: true
我试图将当前用户 ID 插入 votes 对象,但只是得到一个静态字符串,其中显示 user_id: true。如何将实际的 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');
}
}
}
最佳答案
您的问题是您正在使用以下表示法:
let votes = { user_id: true};
这里的user_id
被视为属性的名称,而不是保存属性名称的变量。快速解决方法是使用 []
表示法:
let votes = { };
votes[user_id] = true;
关于javascript - 如何防止重复投票react-firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52213249/