我对每个循环都有不同的文档 ID,当我在 forEach 循环中查询时,查询正在运行,但没有将 obj 推入数组 function getAllDonations() { donations = []; const user_session_data = sessionStorage.getItem('LoginInfo'); const parse_user_login_data = JSON.parse(user_session_data); let TABLE_NAME = "donation_favourites"; let get_requests_qry = App.db.collection(TABLE_NAME); get_requests_qry.where('user_id', '==', parse_user_login_data.user_id).get().then(snapshot => { let changes = snapshot.docChanges(); changes.forEach(change => { var one_item = change.doc.data(); let TABLE_NAME1 = "donation_requests"; let get_requests_qry1 = App.db.collection(TABLE_NAME1); get_requests_qry1.doc(one_item.donationId).get().then(snapshot => { donations.push(snapshot.data()) }); }); console.log("checking the data",donations.length) //this length is not coming }); }
1 回答
POPMUISE
TA贡献1765条经验 获得超5个赞
如果您想读取正在使用的文件 forloop 但不建议用于小循环的大循环,如果您想并行读取文件,则可以使用 forEach
你也可以用asyncandawait代替 forLoop
await Promise.all(changes.map(async (change) => {
var one_item = change.doc.data()
let TABLE_NAME1 = "donation_requests";
let get_requests_qry1 = App.db.collection(TABLE_NAME1);
var snapshot1 = await get_requests_qry1.doc(one_item.donationId).get()
donations.push(snapshot1.data())
}));
添加回答
举报
0/150
提交
取消