我正在嘗試使用firebase 9模塊化 sdk 提交批量寫入。此操作包括創建一個post檔案,最好使用自動生成的 id。
const batch = writeBatch(db)
const postRef = doc(db, 'posts') // this fails, must pass and id
const threadRef = doc(db, 'threads', post.threadId)
batch.set(postRef, post)
batch.update(threadRef,
'posts', arrayUnion(postRef.id),
'contributors', arrayUnion(state.authId))
await batch.commit()
在Firebase 8中,可以使用以下代碼獲取具有隨機生成 id 的檔案參考。
firebase.firestore().collection(nameOfTheCollection).doc()
我正在嘗試使用Firebase 9模塊化 sdk 完成同樣的事情,但我找不到任何方法來做到這一點。是否可以在不先使用Firebase 9創建檔案的情況下獲得具有隨機生成 id的檔案參考?
uj5u.com熱心網友回復:
您可以使用該doc()函式并將 aCollectionReference作為第一個引數傳遞,而無需任何路徑段,如下所示:
import { doc, collection } from "firebase/firestore";
const docRef = doc(collection(db, "colName")); // Don't specify an ID yourself
console.log(`New Document ID: ${docRef.id}`);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/492821.html
標籤:javascript 火力基地 谷歌云火库
