foreach语句中的Firestore复合查询 [英] Firestore Compound Queries inside foreach statement
问题描述
如果可能的话,我想要一些帮助,我想在2个foreach语句中进行Firestore复合查询,因为我需要获取与循环数组中的内容相匹配的所有相关数据。
I would like some help if possible, I want to do a Firestore compound query inside 2 foreach statements as I need to get all relevant data back that matches the contents in the arrays being looped through.
arenas.forEach((arenasElement: any) => {
sports.forEach((sportsElement: any) => {
firebaseAdmin.firestore().collection('leagues')
.where('arenaUid', '==', arenasElement)
.where('sportUid', '==', sportsElement)
.get()
.then((querySnapshot: any) => {
querySnapshot.docs.map((element: any) => {
leaguesArray.push(element.data())
})
})
.catch((error) => {
console.log(error)
res.send(error)
})
})
})
I需要所有相关结果存储在数组中,因为它们都是对象&当所有查询都完成执行时,我想返回该数组,因为我需要所有数据。
I need all the relevant results to be stored in the array as all of them are objects & I want to return that array when all of the queries are finished executing as I need all the data.
我已经尝试过查看promises&包装它,但我在这件事上没有成功。
I've tried looking at promises & wrapping it but I had no success in the matter.
任何帮助都将很乐意赞赏!
Any help would be gladly appreciated!
编辑:以上回答如下,但我正在努力解决另一个问题
The above got answered with below but I am struggling with another function I have
export const getSports = firebaseFunctions.https.onRequest((req: any, res: any) => {
cors(req, res, () => {
let sportsUidArray: any = []
let sportsArray: any = []
let arenas: any = []
let promises: any = []
arenas = req.body.arenas
arenas.forEach((arenasElement: any) => {
promises.push(
firebaseAdmin.firestore()
.collection('leagues')
.where('arenaUid', '==', arenasElement)
.orderBy('title')
.get()
)
})
Promise.all(
promises
).then((querySnapshots: any) => {
querySnapshots.forEach((snapshotElement: any) => {
snapshotElement.docs.map((element: any) => {
sportsUidArray.push(element.data().sportUid)
})
})
sportsUidArray = Array.from(new Set(sportsUidArray))
promises = []
sportsUidArray.forEach((sportIdElement: any) => {
promises.push(
firebaseAdmin.firestore()
.collection('sports')
.doc(sportIdElement)
.get()
)
})
Promise.all(
promises
).then((querySnapshots2: any) => {
querySnapshots2.forEach((snapshotElement: any) => {
/* Issue with line below */
snapshotElement.docs.map((sportsElement: any) => {
sportsArray.push({ id: sportsElement.id, data: sportsElement.data() })
})
})
res.send(sportsArray)
}).catch((err: any) => {
res.send(err)
console.error(err)
})
}).catch((err: any) => {
res.send(err)
console.error(err)
})
})
})
我从Firebase收到错误:
Error I'm receiving from Firebase:
TypeError: Cannot read property 'map' of undefined
推荐答案
这应该可以解决问题:
var promises = [];
arenas.forEach((arenasElement: any) => {
sports.forEach((sportsElement: any) => {
promises.push(firebaseAdmin.firestore().collection('leagues')
.where('arenaUid', '==', arenasElement)
.where('sportUid', '==', sportsElement)
.get());
})
})
Promise.all(promises).then((snapshots: any) => {
var leaguesArray = [];
snapshots.forEach((querySnapshot: any) => {
querySnapshot.docs.map((element: any) => {
leaguesArray.push(element.data())
})
})
res.send(JSON.stringify(leaguesArray));
})
.catch((error) => {
console.log(error)
res.send(error)
})
你会请注意我的更改非常小:
You'll note that my changes are pretty small:
- 使用加载数据的代码创建一个promises数组。
- 输入你的代码
t hen()
到一个然后()
在所有数据加载后触发(使用Promise.all()
)。
- Create an array of promises with the code that load the data.
- Put the code of you
then()
into a singlethen()
that fires after all data has loaded (withPromise.all()
).
这篇关于foreach语句中的Firestore复合查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!