[英]How can i add a collection to a document in firestore coding?
嗨,我有一個應用程序,當用戶注冊時,我使用這個 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 )
}
因此,當用戶注冊時,我通過執行以下操作創建具有自動生成的 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) })
}
現在我想在用戶調用“任務”中創建一個文檔並添加一個自動 ID、一個名稱和一個日期。 我該怎么做,我四處尋找並找不到解決方案數據庫的結構將是 Collection users-document user(userid name and mail)-collection "tasks"-task document(id name and date) 謝謝!! !!
當您向其中添加第一個文檔時,會自動創建一個集合。
如果要指定文檔 ID,則需要執行此操作,
db.collection('users').doc(user).collection(‘tasks’).doc(task).set({ id : this.id, name : this.name, date : this.date})
如果您想讓 Cloud Firestore 為您自動生成 ID。 你可以通過調用 add() 來做到這一點:
db.collection('users').doc(user).collection(‘tasks’).add({id: this.id, name: this.name, date: this.date })
本頁面詳細介紹了如何將數據添加到 Cloud Firestore
您還可以進行 REST API 調用,以在項目 YOUR_PROJECT_ID 下使用集合中的 ID 創建文檔的路徑。 為此,您將使用以下結構:
/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) /文件/城市/洛杉磯
要創建 Firestore 嵌套的 collections,您還可以使用 HTTP POST 請求,如本文檔中所述。 要詳細了解如何通過指定路徑參數 parent 和 collectionID 添加 collections 和文檔的過程,請遵循此線程。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.