How to add data to a specific uid in Firestore Database?
It looks like you're close. Right now, you aren't returning anything from post
, though. I think you also mean to be getting the text
values from each UITextField
instead of just declaring String
s with the names of the fields.
@IBAction func newBookSaveButtonTapped(_ sender: Any) {
guard let uid = Auth.auth().currentUser?.uid else {
//handle your error here
return
}
self.ref?.child("new reading").child(uid).setValue(post())
}
func post() -> [String:String] {
return ["bookTitle" : bookTitleTextField.text ?? "",
"bookAuthor" : bookAuthorTextField.text ?? "",
"bookSummary" : bookSummaryTextField.text ?? ""]
}
Adding data to a specific UID in firebase
I've managed to solve the problem, assuming the user is already authenticated within the application: create an IBoutlet for the textfield named "yourtextfieldname" convert the textfield data into an NSSTRING and place the otherstr variable in the setValue parameter. then add the function to the button action.
let userID = FIRAuth.auth()?.currentUser?.uid
//gets current user uid
func test() {
let str = yourtextfieldname.text
let otherstr = str! as NSString
//convert textfield datatype NSString
self.ref.child("users").child(userID!).child("yourtextfieldname").setValue(otherstr)
}
@IBAction func but(sender: AnyObject) {
//calling test function
updateProfile()
}
Json data structure
{
"users": {
"unique UID": {
"test": "strTest"
}
}
}
Firebase: linking user UID to database
When you register any new user you need to do first register code:
FIRAuth.auth()?.createUserWithEmail(UserEmail, password: UserPassword, completion: { (user, error) in
On the success response you can set the user more details use dictionary
and use SetValue
method by adding Child using following code:
ref.child("users").child(user!.uid).setValue(["Provider": self.txtPassword.text!,"Email": self.txtEmail.text!,"Firstname": self.txtFName.text!,"Lastname": self.txtLname.text!,"Phone": self.txtPhone.text!,])
Here user!.uid is a unique user id child in your main USER tree.
so your firebase tree look like :
and
sample code:
@IBAction func ActionRegister(sender: UIButton) {
let ref : FIRDatabaseReference!
ref = FIRDatabase.database().reference()
FIRAuth.auth()?.createUserWithEmail("abc@abc.com", password: "1234567", completion: { (user, error) in
if((error) != nil)
{
print("Error is = \(error?.localizedDescription)")
}
else
{
// following method is a add user's more details
ref.child("users").child(user!.uid).setValue(["Provider": "1234566", "Email": "abc@abc.com", "Firstname": "nitin", "Lastname": "Gohel", "Phone": "12345678" ])
//following method get the registers user details
ref.child("users").child(user!.uid).observeSingleEventOfType(.Value, withBlock: { snapshot in
let postDict = snapshot.value as! [String : AnyObject]
print("Error is = \(postDict)")
})
}
})
}
Saving data under a specific user UID with Firebase
To save data under userid
, get the userid first:
var user = firebase.auth().currentUser;
var uid=user.uid;
then save the data under the uid:
var ref2 = firebase.database().ref('Providers').child("City").child("Cincinnati");
ref2.child(uid).set({
jobDetails:details
});
more info here:
https://firebase.google.com/docs/database/web/read-and-write
Related Topics
How to Remove Items from an Array When Deselecting a Row in a UItableview
Wrong Value Returned After Formatting Timestamp
Cloudkit - What to Do When a User Adds, Modifies or Deletes an Object While Offline
Validatemenuitem or Menuwillopen Not Called for Nsmenu
Mkpolyline Broken When Using Type Satelliteflyover
How to Show Two Row of Text in a Menu Bar App in MAC Os
Nsundomanager: Capturing Reference Types Possible
Transparency Issues with Repeated Stamping of Textures on an Mtkview
Double Variable in Mkmapitem Array
Swift 3 Cocoa: Use Quicklook to Preview File in Os X
How to Insert UIlabel After UItextfield in UIalertcontroller
Cannot Assign Value of Type 'Menuview' to Type 'some View'
Swiftui Navigationview Starting Inside Itself
Access Safari Bookmarks in Macos Mojave Programmatically
Include Inheritance Constraint in Swift Generic Types