Firebase中的异步函数甚至在函数体完成执行之前就解析了promise



所以我有一个异步函数,它以一个自定义对象数组为参数,然后循环遍历每个对象,并从firestore中获取一些数据。Firestore中的数据将转换为另一个自定义对象,并添加到自定义对象数组中的另一个对象。这些对象的长度由函数返回。

async function getCompsReceived(theInterestedPeople: InterestedPerson[]) {
const whatsNewObjects: WhatsNewObject[] = []
theInterestedPeople.forEach(async (person) => {
const documentsSnapshot = await 
db.collection('Users').doc(person.uid).collection('complimentsReceived')
.orderBy('receivedTime', 'desc')
.limit(documentLimit)
.get()
if (documentsSnapshot.empty) {
console.log(`${person.userName} has no compsReceived`)
return
}

await documentsSnapshot.forEach((newCompReceived: { receiverUid: string; receiverUserName: string; 
complimentsReceivedContent: string | null; hasImage: boolean | null; senderUid: string | null; 
senderUserName: string | null; noOfLikes: number | null; receivedTime: number | null; 
complimentId: string | null} ) => {
//Add each new Compliment to the WhatsNewObjects Array
const whatsNewDoc = new WhatsNewObject(
newCompReceived.receiverUid,
newCompReceived.receiverUserName,
true,
newCompReceived.complimentsReceivedContent,
newCompReceived.hasImage,
newCompReceived.senderUid,
newCompReceived.senderUserName,
newCompReceived.noOfLikes,
newCompReceived.receivedTime,
newCompReceived.complimentId,
'PERSON_COMPLIMENT_RECEIVED'
)
whatsNewObjects.push(whatsNewDoc)
})
console.log(`length of whatsNewObjects after adding ${person.userName}'s compsReceived is      
${whatsNewObjects.length}`)
})
console.log(`returning the length of WhatsNewObjects at getCompsReceived which is ${whatsNewObjects.length}}`)
return whatsNewObjects.length
}

问题是,这个函数总是返回0,并且在Firebase控制台上打印日志语句时,我看到函数体是在函数已经返回了一个类型为Promise的值之后执行的。

有人能帮助我如何让函数在返回whatsNewObjects.length之前等待正文执行吗?

您不应该使用forEach来迭代异步代码,因为它不是await。它只会即发即弃。改为使用for..of

async function getCompsReceived(theInterestedPeople: InterestedPerson[]) {
const whatsNewObjects: WhatsNewObject[] = []
for (const person of theInterestedPeople) {
const documentsSnapshot = await db.collection('Users').doc(person.uid).collection('complimentsReceived')
.orderBy('receivedTime', 'desc')
.limit(documentLimit)
.get();
if (documentsSnapshot.empty) {
console.log(`${person.userName} has no compsReceived`)
return;
}
documentsSnapshot.forEach((newCompReceived: {
receiverUid: string; receiverUserName: string;
complimentsReceivedContent: string | null; hasImage: boolean | null; senderUid: string | null;
senderUserName: string | null; noOfLikes: number | null; receivedTime: number | null;
complimentId: string | null
}) => {
//Add each new Compliment to the WhatsNewObjects Array
const whatsNewDoc = new WhatsNewObject(
newCompReceived.receiverUid,
newCompReceived.receiverUserName,
true,
newCompReceived.complimentsReceivedContent,
newCompReceived.hasImage,
newCompReceived.senderUid,
newCompReceived.senderUserName,
newCompReceived.noOfLikes,
newCompReceived.receivedTime,
newCompReceived.complimentId,
'PERSON_COMPLIMENT_RECEIVED'
);
whatsNewObjects.push(whatsNewDoc);
})
console.log(`length of whatsNewObjects after adding ${person.userName}'s compsReceived is ${whatsNewObjects.length}`);
}
console.log(`returning the length of WhatsNewObjects at getCompsReceived which is ${whatsNewObjects.length}}`);
return whatsNewObjects.length;
}

问题与forEach有关,即使在回调函数中使用async/await,它也会实际运行异步。您可以通过简单地将forEach更改为任何类型的for迭代器来解决这个问题。

for (let index = 0; index < theInterestedPeople.length; index++) {
const person = array[index];
....your code
}
// or
for (const person of theInterestedPeople) {

}
// or

相关内容

  • 没有找到相关文章

最新更新