javascript - Firestore join foreach map - 从两个集合中获取数据
问题描述
嘿,伙计们,我还需要你的帮助 :( 我不知道如何在 Firebase 中加入或做其他事情
我有两个集合:collection("guests") 有一个数据字段“Eventid”和一个数据字段“Userid”在第一步中我选择具有特定 Userid 的所有客人。所以我在 foreach 循环(或作为数组)中获取所有 Eventid!在第二个 Collection 集合('events')中,我选择所有信息并将其写入 JSON 文件。几次后这也有效,这是我的问题!我在函数中运行它,函数在加载事件之前返回。我不知道如何使用它,我尝试使用 await 和 async 或将其拆分为两个函数。Maybye 还有另一种实现方式。
db.collection("guests").where("userid", "==", id).get().then(function(querySnapshot) {
querySnapshot.forEach(function (doc) {
db.collection('events').doc(doc.data().eventid).get().then(function(doc) {
if (doc.exists) {
console.log(doc.id, " => ", doc.data());
} else {
console.log("No such document!");
}
}).catch(function(error) {
console.log("Error getting document:", error);
});
});
});
解决方案
我知道了!
exports.getEvent = functions.https.onRequest(async (req, res) => {
console.log('==NACHEINANDER STARTEN==');
const id = req.query.uid;
var arr = [];
var json = [];
let query = db.collection("guests").where("userid", "==", id);
await query.get().then(querySnapshot => {
querySnapshot.forEach(documentSnapshot => {
arr.push(documentSnapshot.data());
});
});
//console.log(arr);
await processArray(arr)
async function delayedLog(item) {
await db.collection('events').doc(item.eventid).get().then(function(doc) {
console.log(doc.data());
json.push(doc.data());
})
}
async function processArray(array) {
const promises = array.map(delayedLog);
// wait until all promises are resolved
await Promise.all(promises);
console.log('Done!');
}
console.log("hello");
res.status(200).send(JSON.stringify(json)); //, ...userData
});
推荐阅读
- django - Debuggin django 登录 NGINX - DigitalOcean
- angular - ngOnInit 上的 await 函数返回未定义
- javascript - React Native - 格式化文本中的链接
- javascript - 在被覆盖的页面上注入时,我的脚本如何在 HTML 模板中工作?(导入 jQuery)
- google-tag-manager - 使用 Google 跟踪代码管理器添加带有 onload 的脚本
- ruby - 你如何运行 Homebrew 内部测试?
- flutter - Flutter中如何在GroupedListView中进行搜索
- python - 如何在 Python 中有效地创建随机动态图?
- javascript - 控制台错误 - React Native 版本不匹配
- amazon-web-services - 使用 AWS Glue 表分区的 EMR 笔记本中的 Spark SQL 错误