![](/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.