Javascript Firestore多查询
我是nodejs新手,我想在nodejs(云函数)中查询Firestore数据库中的多个查询 最少1个查询,仅当定义了data.query[0,1…]时最多5个查询Javascript Firestore多查询,javascript,node.js,Javascript,Node.js,我是nodejs新手,我想在nodejs(云函数)中查询Firestore数据库中的多个查询 最少1个查询,仅当定义了data.query[0,1…]时最多5个查询 if(data.query[0] !== undefined) { db.collection('example').doc(data.query[0]).listCollections().then(collections => { collections.forEach(collection
if(data.query[0] !== undefined)
{
db.collection('example').doc(data.query[0]).listCollections().then(collections => {
collections.forEach(collection => {
console.log('Found subcollection with id:', collection.id);
allcollections = allcollections + collection.id + '&';
})
});
}
if(data.query[1] !== undefined)
{
db.collection('example').doc(data.query[1]).listCollections().then(collections => {
collections.forEach(collection => {
console.log('Found subcollection with id:', collection.id);
allcollections = allcollections + collection.id + '&';
})
});
}
。。。
最后,我将所有的收藏归还给客户
我的问题是,每个查询都应该返回一个值,我不知道如何运行所有查询并在最后只返回结果(所有集合)
谢谢 您可以尝试使用
它将遵循以下原则:
var queryPromises = [];
if(data.query[0] !== undefined) {
var promise = db.collection('example').doc(data.query[0]).listCollections();
queryPromises.push(promise);
}
if(data.query[1] !== undefined) {
var promise = db.collection('example').doc(data.query[1]).listCollections();
queryPromises.push(promise);
}
if(data.query[2] !== undefined) {
var promise = db.collection('example').doc(data.query[2]).listCollections();
queryPromises.push(promise);
}
if(data.query[3] !== undefined) {
var promise = db.collection('example').doc(data.query[3]).listCollections();
queryPromises.push(promise);
}
if(data.query[4] !== undefined) {
var promise = db.collection('example').doc(data.query[4]).listCollections();
queryPromises.push(promise);
}
Promise.all(queryPromises).then(results => {
// results is an array with the result of each one of the promises
// Do what you have to do
});
请注意文档中的这一细节:
返回的值将按照传递的承诺的顺序排列,而不考虑
完成订单