通过Firebase - Swift中的电子邮件进行迭代

时间:2023-01-23 17:10:54

I am currently trying to iterate through all user emails in firebase, however, whenever I run my code, and try to add the "test@gmail.com" user, there is an error. However, when I try to add my current user's email address, "test1@gmail.com", there is a success.

我目前正在尝试遍历firebase中的所有用户电子邮件,但是,每当我运行我的代码并尝试添加“test@gmail.com”用户时,都会出现错误。但是,当我尝试添加当前用户的电子邮件地址“test1@gmail.com”时,就会成功。

Below is a snippet of my code demonstrating this. B Below is also an image showing the structure of my current database. Note that each user's email is under a unique userID under the "users" part of the database.

下面是我的代码片段,展示了这一点。 B下图也是显示当前数据库结构的图像。请注意,每个用户的电子邮件都位于数据库“users”部分下的唯一userID下。

Iterating through email snippet.

通过电子邮件片段进行迭代。

func searchEmails() {
        var ref : DatabaseReference
        let currentUserID = Auth.auth().currentUser?.uid
        ref = Database.database().reference()




        let userRef = ref.child("users")
        userRef.observeSingleEvent(of: .value, with: { snapshot in
            let enumerator = snapshot.children
            while let rest = enumerator.nextObject() as? DataSnapshot {

                userRef.child(rest.key).child("email").observe(.value, with: { snapshot in

                    print(snapshot.value!)

//                    print("Other rest is this \(otherRest.value!) value")

                    if(snapshot.value as? String == self.shareEmail.text) {
                        SVProgressHUD.showSuccess(withStatus: "Request sent to user!")
                    }
                    else {
                        SVProgressHUD.showError(withStatus: "Email not valid.")

                    }

                })
            }
        })

        SVProgressHUD.dismiss()
    }

通过Firebase  -  Swift中的电子邮件进行迭代

1 个解决方案

#1


1  

Why don't you try this, Might turn out to be much less headache.. :-

你为什么不尝试这个,可能会变得更少头痛..: -

if self.shareEmail.text != "" && self.shareEmail.text.isEmpty == false{

        Database.database().reference().child("users").queryOrdered(byChild: "email").queryEqual(toValue: "somemail").observe(.value, with: {(Snapshot) in

            if Snapshot.exists(){

                // The email that you have been searching for exists in the 
                // database under some particular userID node which can 
                // be retrieved from ....

                print(Snapshot)


            }else{

                // No such email found


            }


        }, withCancel: {(error) in

            // Handle any error occurred while making the call to firebase

        })


    }else{

        //Your textfield must not be empty;.... Handle that error

    }

Note : This is only gonna work if Firebase Security rules allow it... so you might have to work on that on your console... Good luck!

注意:只有在Firebase安全规则允许的情况下才能使用它...所以你可能需要在你的控制台上进行操作......祝你好运!

#1


1  

Why don't you try this, Might turn out to be much less headache.. :-

你为什么不尝试这个,可能会变得更少头痛..: -

if self.shareEmail.text != "" && self.shareEmail.text.isEmpty == false{

        Database.database().reference().child("users").queryOrdered(byChild: "email").queryEqual(toValue: "somemail").observe(.value, with: {(Snapshot) in

            if Snapshot.exists(){

                // The email that you have been searching for exists in the 
                // database under some particular userID node which can 
                // be retrieved from ....

                print(Snapshot)


            }else{

                // No such email found


            }


        }, withCancel: {(error) in

            // Handle any error occurred while making the call to firebase

        })


    }else{

        //Your textfield must not be empty;.... Handle that error

    }

Note : This is only gonna work if Firebase Security rules allow it... so you might have to work on that on your console... Good luck!

注意:只有在Firebase安全规则允许的情况下才能使用它...所以你可能需要在你的控制台上进行操作......祝你好运!