Peter K
Peter K

Reputation: 25

How can I add a new document with fields to a firebase collection with the autoid in swift ui

I would like to add a document to my database collection using autoid with fields that I assign the name to in swift ui .

Upvotes: 1

Views: 908

Answers (1)

Nat
Nat

Reputation: 1812

Get a reference of the database:

var db: Firestore!

then you can reference it with:

db.collection()

For example, if I wanted to add a person's name under a unique id:

db.collection("users").addDocument(data: [
"name": nameTextField.text // Change this to the variable you want to save
]) { err in
if let err = err {
    print("Error writing document: \(err)")
} else {
    print("Document successfully written!")
}
}

If you create the document without an explicit Id, our SDK will auto-generate a random one for you.

The docs are a good source of information too.

Upvotes: 4

Related Questions