[英]How can i add a collection to a document in firestore coding?
Hi there i have an app where when the user sign up i create a collection in my db with firestore using this fn.嗨,我有一个应用程序,当用户注册时,我使用这个 fn 在我的带有 firestore 的数据库中创建一个集合。
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:因此,当用户注册时,我通过执行以下操作创建具有自动生成的 id 的集合:
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.现在我想在用户调用“任务”中创建一个文档并添加一个自动 ID、一个名称和一个日期。 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!!!!
我该怎么做,我四处寻找并找不到解决方案数据库的结构将是 Collection users-document user(userid name and mail)-collection "tasks"-task document(id name and date) 谢谢!! !!
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,如果要指定文档 ID,则需要执行此操作,
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.如果您想让 Cloud Firestore 为您自动生成 ID。 You can do this by calling add():
你可以通过调用 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本页面详细介绍了如何将数据添加到 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.您还可以进行 REST API 调用,以在项目 YOUR_PROJECT_ID 下使用集合中的 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 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) /文件/城市/洛杉矶
For creating Firestore nested collections, you can also use HTTP POST requests like mentioned in this documentation .要创建 Firestore 嵌套的 collections,您还可以使用 HTTP POST 请求,如本文档中所述。 To understand the process in detail on how we can add collections and documents by specifying path parameters parent and collectionID follow this thread .
要详细了解如何通过指定路径参数 parent 和 collectionID 添加 collections 和文档的过程,请遵循此线程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.