Firebase按属性名称查询一个特定对象

时间:2016-07-11 16:16:18

标签: ios swift firebase

我正在设置一个使用firebase的应用。我要做的是在登录时获取一个用户的关联值。我已正确设置种子功能,但我想拉取我在这里创建的用户对象:

 // MARK: - Firebase Register
func firebaseRegister() {

    var allFieldsAreFilled = true

    for textField in requiredTextFields {
        if textField.text == "" {

            allFieldsAreFilled = false

        }
    }

    if allFieldsAreFilled {

        if let email = emailTextField.text, password = passwordTextField.text, givenName = firstNameTextField.text, familyName = lastNameTextField.text, state = stateTextField.text, phoneNumber = phoneNumberTextField.text {

            if password == verifyPasswordTextField.text {

                if email == verifyEmailTextField.text {

                    FIRAuth.auth()?.createUserWithEmail(email, password: password) { (user, error) in

                        if error != nil {

                            print(error)

                        } else {

                            self.ref.child("users").child(user!.uid).setValue(

                                ["email": email,
                                    "password": password,
                                    "givenName":givenName,
                                    "familyName": familyName,
                                    "state": state,
                                    "phoneNumber": phoneNumber]

                            )
                        }
                    }
                }
            }

        } else {

            presentAlert("Please fill all fields")

        }
    }
}

我想通过电子邮件从用户路径中提取孩子。我无法使用查询示例来执行此操作。提前谢谢!

1 个答案:

答案 0 :(得分:1)

以下是我目前使用的代码块,如果用户不存在,它还会将用户保存到我的核心数据中

self.ref.child("users").queryOrderedByChild("email").queryEqualToValue("\(email)").observeEventType(.Value, withBlock: { snapshot in

                    if ( snapshot.value is NSNull ) {

                        print("username does not exist")

                    } else {

                        if !DataController.sharedInstance.usernameExists(email) {

                            if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {

                                for snap in snapshots {

                                    if let dict = snap.value as? Dictionary<String, AnyObject> {

                                        let key = snap.key

                                        if let givenName = dict["givenName"] as? String, familyName = dict["familyName"] as? String, email = dict["email"] as? String, password = dict["password"] as? String, state = dict["state"] as? String, phoneNumber = dict["phoneNumber"] as? String {

                                            DataController.sharedInstance.seedUser(key, email: email, given_name: givenName, family_name: familyName, password: password, state: state, phoneNumber: phoneNumber)

                                        }
                                    }
                                }
                            }
                        }

                        self.currentUser = DataController.sharedInstance.fetchUserByEmail(email)

                    }
                })