问题
I can't save new data ("messages") to current user.
my code for push user info to Firebase
var ref: FIRDatabaseReference! = FIRDatabase.database().reference()
self.ref.child("Users").child("UserID").childByAutoId().setValue(["Name": userName, "Email": userEmail])
code for push messages to Firebase
var messageRef: FIRDatabaseReference = FIRDatabase.database().reference()
let uid = FIRAuth.auth()?.currentUser?.uid
messageRef.child("Users").child("UserID").child(self.uid!).child("Messages").childByAutoId().setValue(["Sender Name": Profile.sharedProfile.Name!,"Date": self.currentDate(),"Time": self.currentTime(), "Text": self.text.text!])
It's what i see in Firebase Database
but i want's like this messages need save in current user
回答1:
Your first block of code is incorrect. If you want to put your Messages
under existed User
, you should use child(self.uid!)
instead of childByAutoId()
, because the second method generates new instance in firebase DB. So your code should looks something like:
var ref: FIRDatabaseReference! = FIRDatabase.database().reference()
self.ref.child("Users").child("UserID").child(self.uid!).setValue(["Name": userName, "Email": userEmail])
来源:https://stackoverflow.com/questions/41634439/how-to-save-data-in-current-uid-in-firebase