我正在制作一個函式來清除嵌套集合中的所有檔案。我已按如下方式訪問它。
const deleteChat = async () => {
const chatSnapShot = await db
.collection("chats")
.doc(router.query.id)
.collection("messages")
.get();
chatSnapShot.forEach((doc) => {
console.log(doc.data());
});
};
我怎樣才能做到這一點?這doc.data()將回傳messages集合內的資料。我嘗試過使用其他解決方案,但沒有奏效。幫助將不勝感激:)
uj5u.com熱心網友回復:
get()讀取該集合中的所有檔案。你需要用來delete()洗掉一個檔案。嘗試重構代碼,如下所示:
const deleteChat = async () => {
const chatSnapShot = await db
.collection("chats")
.doc(router.query.id)
.collection("messages")
.get();
const deletePromises = chatSnapshot.docs.map(d => d.ref.delete());
await Promise.all(deletePromises)
console.log("Documents deleted")
};
這chatSnapShot.docs是一個QueryDocumentSnapshot陣列,您可以使用屬性獲取每個檔案的DocumentReference 。.ref然后檔案參考具有.delete()洗掉檔案的方法。此方法回傳一個 Promise,因此您可以映射一組 Promise 并使用Promise.all()
您還可以使用批量寫入批量洗掉多達 500 個檔案的檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/449810.html
標籤:javascript 火力基地 谷歌云平台 谷歌云火库
