Firebase - 无法执行对后端的调用

时间:2016-07-14 04:41:33

标签: ios swift firebase firebase-authentication

我正在处理一个处理用户注册的功能,在此过程中,检查是否采用了用户输入的所选用户名来通知用户选择不同的用户名。我有以下代码来完成这个场景:

@IBAction func proceedPressed(sender: AnyObject) {

        /**********************Perform Validation************************/
        if(self.emailTxtField.text != "" && self.passwordTxtField.text != "")
        {
            print("Email and Password not empty")
            self.usernameValidation({(result) -> Void in
                if(result == false)
                {
                    print("Result False")
                    self.usernameErrorLabel.text = "Username Taken"
                }else{
                    print("Result True")
                    //Username is available...Proceed
                    self.usernameErrorLabel.text = ""

                    FIRAuth.auth()?.createUserWithEmail(self.emailTxtField.text!, password: self.passwordTxtField.text!) { (user, error) in
                        if(error == nil)
                        {
                            print("Creating User with Email")
                            /*Create the user object as submitted*/
                            self.dbReference.child("users").child(user!.uid).setValue(["username": self.emailTxtField.text!,"name":self.nameTxtField.text!, "email":self.emailTxtField.text!, "mobile":self.mobileTxtField.text!, "homeAddress":"N", "workAddress":"N", "otherAddress":"N", "profilePictureRef":"N","telephone":"0","friendsCount":0, "retailersCount":0])


                        }else{
                            print("Error occured: \(error?.description)")
                        }
                    }//end of createUserWithEmail
                }
            })

        }else{
            print("Error: Email or Password field is empty")
        }

    }

并检查用户名:

func usernameValidation(completion: (result: Bool) -> Void)
    {
        print("Username is: \(self.usernameTxtField.text!)")

        dbReference.child("usernamesTaken").queryOrderedByValue().queryEqualToValue(self.usernameTxtField.text!).observeEventType(.Value, withBlock: { (snapshot: FIRDataSnapshot!) -> Void in
            print(snapshot.childrenCount)
            if(snapshot.childrenCount == 0)
            {
                print("result is true in username validation")
                //Username Available
                completion(result:true)
            }else{
                print("result is false in username validation")
                //Username Taken
                completion(result:false)
            }
        })
    }

上面的问题是完整的代码似乎没有执行。按下按钮时,我在控制台中收到以下消息: - 电子邮件和密码不为空 - 用户名是:[在usernameTxtField.text中输入的值 然后再没有。虽然我写了很多打印语句来试图看看它停止了什么,但这是代码在打印语句方面最远的。

这里有什么问题我错过了吗?

提前致谢。

1 个答案:

答案 0 :(得分:1)

我做了一些测试,然后通过xcode控制台发现了这个问题。我从firebase网站复制了以下内容以测试获取数据:

ref.child("users").child(userID!).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
  // Get user value
  let username = snapshot.value!["username"] as! String
  let user = User.init(username: username)

  // ...
  }) { (error) in
    print(error.localizedDescription)
}

以上显示的错误是"权限被拒绝"。之后,我在控制台的数据库部分编辑了规则,并允许.read和.write这样做。我想我会发布细节以防其他人被卡住。