Adding Data to a Specific UId in Firebase

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 Strings 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 :

Sample Image

and

Sample Image

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



Leave a reply



Submit