检索并检查特定用户是否输入电子邮件地址是否存在于Firebase中 [英] Retrieve and check whether a particular user enter email is present in firebase

查看:135
本文介绍了检索并检查特定用户是否输入电子邮件地址是否存在于Firebase中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个android应用程序,并使用Firebase来存储我的数据。我想查询firebase实例,以检查用户输入的电子邮件地址是否与Firebase中的某个电子邮件地址相匹配。附加Firebase后端数据。





我的要求是,我想通过监护人(它是数据库实例的直接子节点)来检查用户是否进入电子邮件匹配该孩子的任何一个电子邮件地址。在附图中,如果用户输入的电子邮件匹配ram@gmail.com或另一个,我想做一些事情。

  databaseReference = FirebaseDatabase.getInstance()。getReference()。child(guardians); (DataSnapshot data:dataSnapshot.getChildren()){$ b $ 
public void onDataChange(DataSnapshot dataSnapshot){

databaseReference.addListenerForSingleValueEvent(new ValueEventListener(){
@Override
b如果(**我无法弄清楚如何通过电子邮件**搜索整个监护人孩子){

} else {

}
}
}

@Override
public void onCancelled(DatabaseError databaseError){
$ b}
});

我无法弄清楚如何通过整个监护人孩子搜索电子邮件是否匹配用户输入的邮件。任何帮助表示赞赏。如果你使你当前的方法工作(这是一个添加正确的问题的问题)。解决方案 如果语句),你会下载整个用户列表,只是为了检查一个特定的电子邮件地址是否正在使用。这是非常浪费的带宽,尤其是当你的用户列表增长。



您应该改为使用Firebase数据库查询仅返回具有所需电子邮件地址的用户:

  databaseReference = FirebaseDatabase.getInstance()。getReference()。child(guardians); 
Query query = databaseReference.orderByChild(guardianEmail)。equalTo(guardian@example.com);
query.addListenerForSingleValueEvent(new ValueEventListener(){
@Override $ b $ public void onDataChange(DataSnapshot dataSnapshot){$ b $ if(dataSnapshot.exists()){
.. 。电子邮件地址已被使用


$ b @Override
public void onCancelled(DatabaseError databaseError){
throw databaseError.toException() ; //不要忽略错误
}
});

务必 guardianEmail 字段添加索引否则,你仍然会最终下载查询的所有数据。



请注意,这个主题已经被覆盖了很多次,有更好的方法来做到这一点检查。其中大部分涉及创建一个所谓的倒排索引,在这里你使用用户的(编码)电子邮件地址作为关键。通过这种结构,您可以防止Firebase服务器端安全规则中出现重复,效率更高。



有关此方法和其他方法的更多信息,请参阅:




I am developing an android application and using Firebase to store my data. I want to query the firebase instance to check whether the user entered email address matches one of the email in Firebase. Attaching the Firebase backend data.

My requirement is, I want to loop through the "guardians", which is the direct child of the Database instance and check whether the user entered email matches any one of the email address in that child. In the attached image, if the user entered email matches either "ram@gmail.com" or the other one, I want to do something.

databaseReference = FirebaseDatabase.getInstance().getReference().child("guardians");
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot data: dataSnapshot.getChildren()){
                if (**I am unable to figure out how to search through entire guardians child for the email**) {

                } else {

                }
              }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

I am unable to figure out how to search through the entire guardians child to see whether the email matches the user entered email. Any help is appreciated. Thanks in advance.

解决方案

If you made your current approach work (it's a matter of adding the correct if statement) you'd be downloading the entire list of users, just to check if a specific email address is in use. This is incredibly wasteful of bandwidth, especially as your user list grows.

You should instead use a Firebase Database query to only return the user with the requested email address:

databaseReference = FirebaseDatabase.getInstance().getReference().child("guardians");
Query query = databaseReference.orderByChild("guardianEmail").equalTo("guardian@example.com");
query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
                ... the email address is already in use
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            throw databaseError.toException(); // don't ignore errors
        }
    });

Be sure to add an index for the guardianEmail field, as otherwise you'll still end up downloading all data for the query.

Note that this topic has been covered quite a few times before, and there are better ways to do this check. Most of these involve creating a so-called inverted index, where you use the (encoded) email address of the user as the key. With that structure you can prevent duplicates in Firebase's server-side security rules, which is even more efficient.

For more on this and other approaches, see:

这篇关于检索并检查特定用户是否输入电子邮件地址是否存在于Firebase中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆