我正在尝试将新值插入Firebase实时数据库。但是每次我“注册”新用户时,我的数据库数据都会被新用户替换。我对如何做到这一点完全感到困惑。
这是我的代码。
//Create a new user with txtfield email & password
Auth.auth().createUser(withEmail: email, password: password) { authResult, error in
//Printing error if any
if error != nil
{
print(error as Any)
return
}
//Getting database reference
let ref = Database.database().reference()
//Values to insert into database
let values = ["Username": self.username, "Email": self.email, "Name1": self.name1, "Name2": self.name2, "Name3": self.name3, "Name4": self.name4, "Name5": self.name5, "Name6": self.name6, "Dog1": dog1, "Dog2": dog2, "Dog3": dog3]
//Updating the child values
ref.updateChildValues(values, withCompletionBlock: { (err, ref) in
//Printing error if any
if err != nil
{
print(err as Any)
return
}
//Succeded if no errors
print("Saved user successfully in Firebase database!")
})
我认为问题出在
ref.updateChildValues(values, withCompletionBlock: { (err, ref) in
但是我不知道该如何替换。因此,简而言之。 每次创建新用户时,数据库数据都会被替换。我基本上希望它为每个用户创建一个新的“行”。
答案 0 :(得分:4)
您需要将项目插入像users/userID
这样的孩子下
guard let firUser = authResult.user else { return }
let userID = firUser.uid
let ref = Database.database().reference()
let user = ref.child("users/\(userID)")
user.updateChildValues////
数据库
> users
> njfjnjnf889489489 // some user uid
> name
> mail
> 322jnjnf889489483 // some user uid
> name
> mail