![](/img/trans.png)
[英]Firebase Cloud Functions: Transactions function not returning promise?
[英]Firebase Firestore transactions on cloud functions ( getAll method on the callback function first param ) How to use it?
我一直在努力找出如何在 runTransation 函數的回調函數的第一個參數上准確使用 getAll 函數。
doc firebase doc 僅顯示如何使用 get 函數檢索單個文檔,但我想根據集合中的多個 where 語句檢索多個文檔。
問題是如何使用下面的getAll函數?
export const match = functions.firestore
.document('waitingList/{userId}').onCreate(async (snapshot, context) => {
app.firestore().runTransaction(async transaction => {
transaction.getAll( //?); // ???
});
});
更新 1 | 23/4/2022
我想出了一個方法(但我對它並不完全滿意,因為從 firestore 讀取重復)。
解決方法如下
app.firestore().runTransaction(async transaction => {
const docRefs: FirebaseFirestore.DocumentReference<any>[] = [];
(await firestore
.collection('collectionName')
.limit(100)
.get())
.forEach((doc) => {
docRefs.push(doc.ref);
}); //This is reading the Database for 100 docs
const users = await transaction.getAll(...docRefs); // This also is reading the database for the same 100 docs (But in a transaction context)
});
如果有人知道如何在交易中只閱讀一次文檔,請提供解決方案。
像這樣的東西:
transaction.getAll(...docRefs).then((docs) => {
docs.forEach((doc) => {/*...*/}
}
正如@Abobker 已經說過的,我同意目前這將是基於集合中的多個 where 語句檢索多個文檔的最佳方式:
app.firestore().runTransaction(async transaction => {
const docRefs: FirebaseFirestore.DocumentReference<any>[] = [];
(await firestore
.collection('collectionName')
.limit(100)
.get())
.forEach((doc) => {
docRefs.push(doc.ref);
}); //This is reading the Database for 100 docs
const users = await transaction.getAll(...docRefs); // This also is reading the database for the same 100 docs (But in a transaction context)
});
雖然不是最有效的解決方案,但它確實解決了問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.