无法合并在火力地堡数据库中的两个火力点阵列

问题描述 投票:0回答:1

你好,我是比较新的雨燕/火力地堡,我挣扎,这样将​​downloadURL看到当中的姓名和电子邮件字段都合并两个数组。一个函数将名字和通过按钮的电子邮件点击另一个是另一个函数保存的URL。当我尝试和合并他们,我得到这个(如下图所示)。这里是我的代码:

@IBAction func createAccountAction(_ sender: AnyObject) {

    let Users = Database.database().reference().child("Users")

    let userDictionary : NSDictionary = ["email" : emailTextField.text as String!, "Name": nameTextField.text!]

    Users.childByAutoId().setValue(userDictionary) {
        (error, ref) in

        if self.emailTextField.text == "" {
            let alertController = UIAlertController(title: "Error", message: "Please enter your email and password", preferredStyle: .alert)

            let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
            alertController.addAction(defaultAction)

            self.present(alertController, animated: true, completion: nil)

        } else {
            Auth.auth().createUser(withEmail: self.emailTextField.text!, password: self.passwordTextField.text!) { (user, error) in

                if error == nil {
                    print("You have successfully signed up")
                    //Goes to the Setup page which lets the user take a photo for their profile picture and also chose a username

                    var imgData: NSData = NSData(data: UIImageJPEGRepresentation((self.profilePicture?.image)!, 0.8)!)
                    self.uploadProfileImageToFirebase(data: imgData)


                    let vc = self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController
                    self.present(vc, animated: true, completion: nil)

                } else {
                    let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)

                    let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
                    alertController.addAction(defaultAction)

                    self.present(alertController, animated: true, completion: nil)
                }
            }
        }
    }
}
 func addImageURLToDatabase(uid:String, values:[String:AnyObject]){
    let Users = Database.database().reference().child("Users")
    let ref = Database.database().reference(fromURL: "https://example.firebaseio.com/")

    Users.updateChildValues(values) { (error, ref) in
        if(error != nil){
            print(error)
            return
        }
        self.parent?.dismiss(animated: true, completion: nil)
    }

}

enter image description here

swift firebase firebase-realtime-database
1个回答
1
投票

像这样的东西是你想要的。我删除从功能的几个变量,但你也可以添加对方。我只是想确保代码编译。

@IBAction func createAccountAction(_ sender: AnyObject) {

    let usersRef = Database.database().reference().child("Users")

    let userDictionary : NSDictionary = ["email" : emailTextField.text!, "Name": nameTextField.text!]

    if emailTextField.text == "" {
        let alertController = UIAlertController(title: "Error", message: "Please enter your email and password", preferredStyle: .alert)
        let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
        alertController.addAction(defaultAction)
        self.present(alertController, animated: true, completion: nil)
    } else {
        Auth.auth().createUser(withEmail: self.emailTextField.text ?? "", password: self.passwordTextField.text ?? "") { (result, error) in
            if error != nil {
                let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
                let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
                alertController.addAction(defaultAction)
                self.present(alertController, animated: true, completion: nil)
                return
            }
            guard let user = result?.user else { return }

            // HERE YOU SET THE VALUES

            usersRef.child(user.uid).setValue(userDictionary, withCompletionBlock: { (error, ref) in
                if error != nil { print(error); return }
                self.addImageURLToDatabase(uid: user.uid, values: ["Put": "Your Values Here" as AnyObject])
            })

        }
    }
}
func addImageURLToDatabase(uid:String, values:[String:AnyObject]){
    let usersRef = Database.database().reference().child("Users").child(uid)
    usersRef.updateChildValues(values) { (error, ref) in
        if(error != nil){
            print(error)
            return
        }
        self.parent?.dismiss(animated: true, completion: nil)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.