Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
503 views
in Technique[技术] by (71.8m points)

compare textfield.text to firebase string swift

I have a database in Firebase that will have individual user nodes. In each user's node will be data pertaining to them and will be private. In addition to that I want to create a node that is JUST a collection of registered emails. The reason is when a user is on the Sign In VC and the user types an email in..If the email is already registered an image view will turn green. However, if the email is not in the database (or if it doesn't match email address format) the image will be red.

A previous answer on my previous question(s) illustrated that I need to change the '.' to a ',' in email addresses. So @gmail.com would be stored as gmail,com

I have that down.

FIRAuth.auth()?.createUser(withEmail: email, password: password, completion: { (user, error) in

    if error == nil {

      let email = firstContainerTextField.text ?? ""

      let newString = email.replacingOccurrences(of: ".", with: ",", options: .literal, range: nil)

      self.ref.child("users").child("allUsers").child(newString).setValue(true)

      self.ref.child("users").child((user?.uid)!).setValue(["Email": email])

      FIRAuth.auth()!.signIn(withEmail: email,
                             password: password)

    } else {
      //registration failure
    }

This is the code from the New User VC (partial).

So the node that says "users" and "allUsers" looks like this on Firebase Console

users
    allUsers
        bob@bob,com: true
        ted@ted,com: true

The 'true' part was just so I could get the bob@bob,com onto the database...the true part will never be used for anything.

On the log in VC I honestly cannot figure out what to do

A previous answer said to use

hasChildren() 

And I used that and then googled what to do with that and I tried using something like this

ref.child("users").child("allUsers").queryEqual(toValue: newString)
  .observe(.value, with: { snapshot in

if snapshot.hasChildren() {

    for child in snapshot.children.allObjects as! [FIRDataSnapshot] {
    ....


}



  });

But I just cannot seem to get anywhere with it.

How can I simply see if a textfield.text == an email already stored in firebase?

(I did convert the '.' to ',' when comparing)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...