如何使用用户输入在Firebase中命名孩子?

时间:2021-02-22 06:47:42

I'm trying to create a simple user database on Firebase for iOS. I have two textfields, one that takes the user's email address, and the other that takes the user's password. I want the email to be the user's uid. I try to accomplish this by creating a variable called "email" which I set equal to emailTextField.text! Then I implement the code that generates child(s) in Firebase, like so:

我正在尝试在Firebase for iOS上创建一个简单的用户数据库。我有两个文本字段,一个用于获取用户的电子邮件地址,另一个用于获取用户的密码。我希望电子邮件成为用户的uid。我尝试通过创建一个名为“email”的变量来实现这一点,我设置的变量等于emailTextField.text!然后我实现了在Firebase中生成子代码的代码,如下所示:

ref = Database.database().reference()

ref.child("users").child(email).setValue(true)

Here's my problem: creating a child and naming it using a variable, doesn't work. I can do the following just fine:

这是我的问题:创建一个子项并使用变量命名它不起作用。我可以做到以下几点:

ref.child("users").child("email")

but once I take away the quotation marks around "email" so that it becomes the variable, the program crashes. How can I use user input to name a child in Firebase?

但是一旦我拿走“电子邮件”周围的引号使它成为变量,程序就会崩溃。如何使用用户输入在Firebase中命名孩子?

1 个解决方案

#1


0  

Firebase node / document names do not support the same character set which email addresses support, for example, the . symbol in an email address would make the child name invalid.

Firebase节点/文档名称不支持电子邮件地址支持的相同字符集,例如。电子邮件地址中的符号会使子名称无效。

If you tried setting a child as ref.child("users").child("xyz@xyz.xyz") you should see the same error.

如果您尝试将子项设置为ref.child(“users”)。child(“xyz@xyz.xyz”),您应该会看到相同的错误。

If you absolutely need to use the email address as the node name, I recommend encoding the email in a way which is compatible with the firebase node name rules.

如果您绝对需要使用电子邮件地址作为节点名称,我建议您以与firebase节点名称规则兼容的方式对电子邮件进行编码。

Link to the rules

链接到规则

Edit: If you are using firebase auth, the normal pattern is too use the UID returned by the authenticated user object as the node name, not the email address entered from the textfield.

编辑:如果您使用的是firebase身份验证,则正常模式也会使用经过身份验证的用户对象返回的UID作为节点名称,而不是从文本字段输入的电子邮件地址。

A quick example:

一个简单的例子:

Auth.auth().signIn(withEmail: email, password: password) { (user, error) in
  if let user = user {
        ref.child("users").child(user.uid).setValue(true)
  }
}

#1


0  

Firebase node / document names do not support the same character set which email addresses support, for example, the . symbol in an email address would make the child name invalid.

Firebase节点/文档名称不支持电子邮件地址支持的相同字符集,例如。电子邮件地址中的符号会使子名称无效。

If you tried setting a child as ref.child("users").child("xyz@xyz.xyz") you should see the same error.

如果您尝试将子项设置为ref.child(“users”)。child(“xyz@xyz.xyz”),您应该会看到相同的错误。

If you absolutely need to use the email address as the node name, I recommend encoding the email in a way which is compatible with the firebase node name rules.

如果您绝对需要使用电子邮件地址作为节点名称,我建议您以与firebase节点名称规则兼容的方式对电子邮件进行编码。

Link to the rules

链接到规则

Edit: If you are using firebase auth, the normal pattern is too use the UID returned by the authenticated user object as the node name, not the email address entered from the textfield.

编辑:如果您使用的是firebase身份验证,则正常模式也会使用经过身份验证的用户对象返回的UID作为节点名称,而不是从文本字段输入的电子邮件地址。

A quick example:

一个简单的例子:

Auth.auth().signIn(withEmail: email, password: password) { (user, error) in
  if let user = user {
        ref.child("users").child(user.uid).setValue(true)
  }
}