将textfield.text与firebase字符串swift进行比较 [英] compare textfield.text to firebase string swift
问题描述
以前对我以前的问题的回答(s)说明我需要在电子邮件地址中将'。'更改为','。因此,@ gmail.com将被存储为gmail,com
我已经下了。
如果错误== nil {
(),createUser(withEmail:email,password:password,completion:{(user,error)in
){code> FIRAuth.auth
let email = firstContainerTextField.text ??
let newString = email.replacingOccurrences(of:。,with:,,options:.literal,range:nil)
$ b $ self.ref.child(users)。child(allUsers)。child(newString).setValue(true)
self.ref.child(用户)。child((user?.uid)!)。setValue([Email:email])
FIRAuth.auth()!. signIn(withEmail:email,
密码:密码)
}其他{
//注册失败
}
这是新用户VC(部分)的代码。
所以说users和allUsers的节点看起来像这样在Firebase控制台上
用户
allUs ers
bob @ bob,com:true
ted @ ted,com:true
<真正的部分就是这样,我可以把bob @ bob,com放到数据库中,真正的部分永远不会被用到任何东西上。
在VC的日志中,我真的不知道该怎么办了。/ b>
以前的回答说使用
<$ p $ > hasChildren()
然后我用google搜索了一下(
),我尝试过使用类似这样的东西
$ ref $ {code $ ref.child(users)。child(allUsers ).queryEqual(toValue:newString)
.observe(.value,其中:$快照
$ b $如果snapshot.hasChildren(){
for child in snapshot.children.allObjects as! [FIRDataSnapshot] {
....
}
});
但是我似乎无法得到任何结果。
我怎样才能简单地看到一个textfield.text ==一个电子邮件已经存储在firebase中?
(我已经将'。'转换为' '比较时)
请不要使用电子邮件地址作为键。电子邮件地址是动态的,并且可能会改变(如果用户想要改变它),如果他们这样做了,那么你将会手忙脚乱,因为直接使用该电子邮件的每个节点都将被删除并重新创建。 / p>
最好的做法是将key和它所包含的数据分开。
这里是使用的结构
电子邮件
-Yiaisjpa90is
电子邮件:dude@test.com
-Yijs9a9js09a
email:thing@test.com
然后您只需查询电子邮件节点寻找和处理,如果存在的话。
和一些代码
<$ c $ queryEqual(toValue:dude@test.com)
.observe(.value,其中:{snapshot in $ b $ if snapshot.value is NSNull {
print(快照为空,没有找到电子邮件)
} else {
print(email found,YIPEE)
}
$ b $ p
$ b为了保持完整性,使用
$ b $ pre $如果snapshot.exists(){
print(found it)
} else {
print (找不到电子邮件)
}
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)
Please don't use email addresses as keys. Email addresses are dynamic and may change (as in if the users wants to change it) and if they do, you'll have a mess on your hands as every node that directly used that email would have be deleted and re-created.
Best practice is to disassociate key's from the data they contain.
Here's the structure to use
emails
-Yiaisjpa90is
email: "dude@test.com"
-Yijs9a9js09a
email: "thing@test.com"
then you simply query the email node for the email you are looking for, and handle accordingly if it exists.
And some code
emailsRef.queryOrdered(byChild: "email").queryEqual(toValue: "dude@test.com")
.observe(.value, with: { snapshot in
if snapshot.value is NSNull {
print("the snapshot was null, no email found")
} else {
print("email was found, YIPEE")
}
})
For completeness it would be a little more Swifty to use
if snapshot.exists() {
print("found it")
} else {
print("no email found")
}
这篇关于将textfield.text与firebase字符串swift进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!