我們試圖通過將它們推入一個陣列然后回傳它來從我們的資料庫中獲取時間段。該陣列確實根據 Firebase 日志正確填充,但是該函式根本不會正確回傳資料,即使我們看到要回傳的資料也是如此。基本上,執行沒有到達return陳述句。
我們的目標是獲取這張照片中的所有時間段。有什么巧妙的方法可以做到這一點嗎?

exports.getTimeslots = functions.region('europe-west2').https.onCall((data, context) => {
const uid = context.auth.uid;
let array = [];
if (!uid)
throw new functions.https.HttpsError('no-userid', 'The requested user was not found');
else
return admin.firestore().collection('users').doc(uid).collection('modules').where('name', '!=', '').get().then(snapshot => {
snapshot.forEach(async doc => {
await admin.firestore().collection('users').doc(uid).collection('modules').doc(doc.id).collection('timeslots').where('length', '!=', -1).get().then(snapshot2 => {
snapshot2.forEach(doc2 => {
array.push(Object.assign(doc2.data(), {id: doc2.id, modID: doc.id}))
console.log("identifier #1", array)
})
console.log("Got outside");
})
console.log("Got more outside");
})
console.log("Got the most outside")
return ({ data: array });
});
//console.log("I have escaped!")
})
uj5u.com熱心網友回復:
正如@Ragesh Ramesh 所說:“解決方案是讓一切都異步等待。”,我嘗試使用資料結構復制您的代碼,代碼如下:
資料結構:

代碼:
// firebase db
const db = firebase.firestore();
exports.getTimeslots = functions.region('europe-west2').https.onCall((data, context) => {
const getData = async () => {
const uid = context.auth.uid;
let array = [];
if (!uid) {
throw new functions.https.HttpsError('no-userid', 'The requested user was not found');
} else {
const modulesRef = db.collection('users').doc(uid).collection('modules');
const modulesQuery = modulesRef.where('name', '!=', '');
const modulesQuerySnap = await modulesQuery.get();
const moduleDocuments = modulesQuerySnap.docs.map((doc) => ({ id: doc.id }));
for (const moduleDocument of moduleDocuments) {
const timeslotsRef = modulesRef.doc(moduleDocument.id).collection('timeslots');
const timeslotsQuery = timeslotsRef.where('length', '!=', -1);
const timeslotsQuerySnap = await timeslotsQuery.get();
const timeslotDocuments = timeslotsQuerySnap.docs.map((doc) => ({ id: doc.id, data: doc.data() }));
for (const timeslotDocument of timeslotDocuments) {
array.push(Object.assign(timeslotDocument.data, {id: timeslotDocument.id, modID: moduleDocument.id}))
}
}
return ({ data: array });
}
}
return getData()
.then((response) => {
// console.log(response);
return response;
});
}
Firestore 的參考頁面顯示docs快照上的屬性。
運行代碼后,輸出如下:
{
data: [
{
length: 1,
id: '8UIlspnvelEkCUauZtWv',
modID: 'RmL5BWhKswEuMWytTIvZ'
},
{
title: 'Modules',
length: 120,
day: 1,
startTime: 720,
id: 'E5fjoGPyMswOeq8mDjz2',
modID: 'qa15lWTJMjkEvOU74N1j'
},
{
startTime: 360,
title: 'English',
day: 2,
length: 240,
id: '7JHtPSO83flO3nFOc0aE',
modID: 'qa15lWTJMjkEvOU74N1j'
}
]
}
uj5u.com熱心網友回復:
這是您的函式撰寫方式的問題。代替
return ({ data: array });
您的函式有時會回傳。
admin.firestore().collection('users').doc(uid).collection('modules').where('name', '!=', '').get()
這本身就是一個承諾。您在 then 函式中鏈接 async 。解決方案是讓一切異步等待。
const data = await admin.firestore().collection('users').doc(uid).collection('modules').where('name', '!=', '').get()
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/450875.html
