从Firestore提取数据时使用async forEach循环 [英] Use async forEach loop while fetching data from firestore
问题描述
我有一些类似的Firestore数据:
I have firestore data somewhat like this:
"Support": {
"userid":"abcdxyz",
"message": "hello"
}
"Support": {
"userid":"abcdxyz",
"message": "hello"
}
我正在使用nodejs来获取我的数据,我还想显示发送此消息的人的电子邮件地址和姓名.所以我正在使用以下功能:
I am using nodejs to fetch my data and I also want to show the email address and name of the person who sent this message. So I am using following function:
database.collection("support").get().then(async function (collections) {
var data = [];
console.log("data collected");
collections.forEach(async function (collection) {
var temp = {};
var collectionData = collection.data()
var userInfo = await getUserDetails(collectionData.userId)
temp.name = userInfo.name
temp.supportMessage = collectionData.supportMessage
data.push(temp)
console.log("data pushed")
});
console.log("data posted")
return res.status(200).end(JSON.stringify({ status: 200, message: "Support Message fetched successfully.", data: data }))
}).catch(error => {
return res.status(500).end(JSON.stringify({ status: 500, message: "Error: " + error }))
});
以下是日志的顺序:收集的数据,发布的数据,推送的数据
Here the sequence of logs is following: data collected, data posted, data pushed
我想要这样的顺序:收集的数据,推送的数据(x次),发布的数据
I want the sequence like this: data collected, data pushed (x times), data posted
推荐答案
我在@estus评论的帮助下解决了我的答案.
I solved my answer with the help of @estus comment.
信用:@estus
var data = [];
var tempCollection = [];
collections.forEach(collection => {
tempCollection.push(collection.data());
});
for (collection of tempCollection) {
var temp = {};
var userInfo = await getUserDetails(collection.userId)
temp.name = userInfo.name
temp.supportMessage = collection.supportMessage
data.push(temp)
}
它很容易解决了我的问题.
It solved my problem very easily.
这篇关于从Firestore提取数据时使用async forEach循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!