Hi there i have an app where when the user sign up i create a collection in my db with firestore using this fn.
const createUser = async ( collection , data ) => {
//adding data to a collection with automatic id
)
await setDoc(
doc( firestore, collection, data.id) , data )
//console.log( ref.id )
}
So when the user signup i create a collection with an auto generated id by doing this:
const SignupHandler = ( email, password, username ) => {
setSignupError(null)
createUserWithEmailAndPassword( FBauth, email, password )
.then( ( userCredential ) => {
createUser('users', {id: userCredential.user.uid, email:userCredential.user.email, displayName:username})
console.log(username)
console.log(userCredential)
setUser(userCredential)
setAuth( true )
} )
.catch( (error) => { setSignupError(error.code) })
}
Now i want to create a document inside the user call "tasks" and add an auto id, a name and a date. How do i do tha, i have looiking around and cant find the solution The structure of the db would be Collection users-document user(userid name and mail)-collection "tasks"-task document(id name and date) thanks!!!!
A collection is created automatically when you add the first document to it.
If you want to specify the document ID, then you'll need to do this,
db.collection('users').doc(user).collection(‘tasks’).doc(task).set({ id : this.id, name : this.name, date : this.date})
If you want to let Cloud Firestore auto-generate an ID for you. You can do this by calling add():
db.collection('users').doc(user).collection(‘tasks’).add({id: this.id, name: this.name, date: this.date })
This page details how toAdd data to Cloud Firestore
You can also make REST API calls to create a path to a document with the ID in the collection under the project YOUR_PROJECT_ID. For that you would use the following structure:
/projects/YOUR_PROJECT_ID/databases/(default)/documents/cities/LA
To interact with this path, combine it with the base API URL for creating a path to a document with ID LA and collection name 'cities' https://firestore.googleapis.com/v1/projects/YOUR_PROJECT_ID/databases/(default)/documents/cities/LA
For creating Firestore nested collections, you can also use HTTP POST requests like mentioned in this documentation . To understand the process in detail on how we can add collections and documents by specifying path parameters parent and collectionID follow this thread .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.