How to save data in current uid in Firebase

故事扮演 提交于 2021-02-08 08:23:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!