swift - How do I get Firebase to add new users and not overwrite current users (iOS, Xcode 8)? -
this code keeps overwriting database after new user created. code have put in:
func handleregister () { guard let email = emailtextfield.text, let password = passwordtextfield.text, let name = nametextfield.text else { print("form not valid") return } auth.auth().createuser(withemail: email, password: password, completion: {(user: user?, error) in if error != nil { print(error.self) return } guard (user?.uid) != nil else { return } }) let ref = database.database().reference(fromurl: "//firebase link") let usersreference = ref.child("users").child("uid") let values = ["name": name, "email": email] usersreference.updatechildvalues(values, withcompletionblock: { (err, ref) in if err != nil { print(err.self) return } print("successfully saved user in firebase db") } )
i feel i'm missing simple. appreciated.
edit: after
guard (user?.uid) != nil else { return }
put remaining code below it:
let ref = database.database()... //your code //put inside completion block of create user //the rest of code updatechildvalues
and use uid parameter child in users
so become:
let usersreference = ref.child("users").child(uid)
note uid not enclosed double quotes
you have put inside createuser completion block because scope of user?.uid block.
Comments
Post a Comment