如何解决Firebase中的异步功能甚至在功能主体完成执行之前就解决了承诺
因此,我有一个异步函数,该函数将自定义对象的数组作为参数,然后遍历每个对象并从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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。