简体   繁体   中英

How to add a user with setValue from Firebase Reading and writing data in ios

I am a newbie in Swift and I am learning by building a social media application. I am struck at trying to implement self.ref.child("users").child(user.uid).setValue(["username": username]) in my code (from https://firebase.google.com/docs/database/ios/read-and-write ). I have been following the instructions of Kasey Schlaudt and at this point of the tutorial https://youtu.be/GrRggN41VF0?t=619 he tried to add a user with setValue as shown in the Firebase documentation I have linked. The errors I get in the line self.ref.child("users").child(user.uid).setValue(["username": username]) are

Use of unresolved identifier 'user' and Use of unresolved identifier 'username'.

My code so far (with some little changes from the original code in the video in my signInPress function) is

import UIKit
import Firebase
import SwiftKeychainWrapper

class ViewController: UIViewController {
    
    
    @IBOutlet weak var UserImageView: UIButton!
    @IBOutlet weak var usernameField: UITextField!
    @IBOutlet weak var emailField: UITextField!
    @IBOutlet weak var passwordField: UITextField!
    
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
    }
    
    override func viewDidAppear(_ animated: Bool)
    {
        if let _ : Bool =  KeychainWrapper.standard.string(forKey: "uid") != nil
        {
            self.performSegue(withIdentifier: "toFeed", sender: nil)
        }
    }
    
    func storeUserData(userID: String)
    {
        //---------------------------problematic line---------------------------
        //from https://firebase.google.com/docs/database/ios/read-and-write
        //from https://youtu.be/GrRggN41VF0?t=619
        self.ref.child("users").child(user.uid).setValue(["username": username])
        ([
            "username": usernameField.text
        ])
    }
    
    @IBAction func signInPress(_ sender: Any)
    {
        //this way you make sure there is a property inside emailField.text and you have a variable you can easily use
        if let email = emailField.text, let password = passwordField.text
        {
            Auth.auth().signIn(withEmail: email, password: password)
            { (result, error) in
                if error != nil && self.usernameField.text!.isEmpty && self.UserImageView.image != nil
                {
                    Auth.auth().createUser(withEmail: email, password: password)
                    { (result, error) in
                        self.storeUserData(userID: (result?.user.uid)!)
                        KeychainWrapper.standard.set((result?.user.uid)!, forKey: "KEY_UID")
                        self.performSegue(withIdentifier: "toFeed", sender: nil)
                    }
                } else
                {
                    KeychainWrapper.standard.set((result?.user.uid)!, forKey: "KEY_UID")
                    self.performSegue(withIdentifier: "toFeed", sender: nil)
                    
                    
                }

            }
       }
    }

}

I would very much appreciate any indication as to why the error does not occur for Kasey and what I might need to change to do the same process. Thank you in advance !

You're actually pretty close, more of a typo issue. See that your storeUserData function is expecting a string called userID? That's what's needed in the line to store that data instead of user.uid

func storeUserData(userID: String) {
   let username = self.usernameField.text

   self.ref.child("users").child(userID).setValue(["username": username])
                           here  ^^^^^^ userID instead of user.uid
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM