我试图弄清楚如何才能在函数结束之前等待执行多个promise。
本质上,这就是我要尝试的方法:
这是我到目前为止尝试过的:
A)主要功能(启动第一级承诺):
export function cleanUpAllData(user) {
const userId = user.uid;
const promises = [];
promises.push(deleteCategoriesData(userId)); // this works fine
promises.push(deleteUserAndGroupData(userId)); // this one has other promises which still run when Promise.all() is finished
Promise.all(promises)
.then(() => {
return "ok"; // this works so far, but not all promises are resolved
})
.catch(errPromises => {
console.log("an error occured during the processing of main promises");
console.log(errPromises, errPromises.code);
return "error";
})
}
B)deleteUserAndGroupData函数(另一个promise正常工作):在用户数据中找到的每个组都启动了另一个promise级别,并且还触发了三级promise(deleteGroup)-其余的都可以正常工作
function deleteUserAndGroupData(userId) {
const promisesUserData = [];
return admin.firestore().collection('users').doc(userId).collection('groups').get()
.then(userGroups => {
userGroups.forEach(userGroupData => {
// delete group data
promisesUserData.push(deleteGroups(userId, userGroupData.id)); // here are other promises that need to be resolved - somewhere is a problem
// delete all Groups in Users (subcollection)
promisesUserData.push(deleteGroupInUser(userId, userGroupData.id)); // this works fine
});
Promise.all(promisesUserData)
.then(() => {
admin.firestore().collection('users').doc(userId).delete()
.then(() => {
return "user data deleted"; // works fine
})
.catch(() => {
console.log("an error occured during deleting of user");
return "error";
});
})
.catch(errPromises => {
console.log("an error occured during the processing of promisesUserData");
console.log(errPromises, errPromises.code);
return "error";
})
})
.catch(errUserGroups => {
console.log(errUserGroups, errUserGroups.code);
return "no groups in User";
});
}
C)deleteGroups功能:删除组中的子集合(可以正常工作),然后,如果没有其他用户(不起作用),则应删除该组
function deleteGroups(userId,groupId) {
const promisesDeleteGroups = [];
// delete groups subcollection data
promisesDeleteGroups.push(deleteGroupRole(userId, groupId));
promisesDeleteGroups.push(deleteGroupUser(userId, groupId));
return Promise.all(promisesDeleteGroups).then(() => {
checkForOthers(groupId);
}).catch(errDeleteGroupSubcollections => {
console.log("an error occured during the processing of promisesDeleteGroups");
console.log(errDeleteGroupSubcollections, errDeleteGroupSubcollections.code);
return "error";
});
}
D)checkForOthers函数-检查子集合中是否有任何条目,并应开始删除组(但不删除)
function checkForOthers(groupId) {
return admin.firestore().collection('groups').doc(groupId).collection('users').get()
.then(groupUsers => {
return "other users exist - group should not be deleted";
})
.catch(errGroupUsers => {
// console.log("no other group members - group can be deleted");
// return "no other group members - group can be deleted";
console.log(errGroupUsers, errGroupUsers.code);
checkForInvitesAndDelete(groupId);
});;
}
E)checkForInvitesAndDelete:首先,如果要邀请另一个用户加入该组,我想删除另一个可能存在或可能不存在的子集合;那么它将触发最终的组删除操作(似乎无效)
function checkForInvitesAndDelete(groupId) {
const promisesInvitesAndDelete = [];
return admin.firestore().collection('groups').doc(groupId).collection('invitations').get()
.then(groupInvitations => {
console.log("delete open Group Invitations");
groupInvitations.forEach(groupInvite => {
promisesInvitesAndDelete.push(deleteGroupInvite(groupId, groupInvite.id));
});
Promise.all(promisesInvitesAndDelete)
.then(() => {
deleteGroup(groupId)
})
.catch(errPromisesInvitesAndDelete => {
console.log("an error occured during the processing of deleting group invites");
console.log(errPromisesInvitesAndDelete, errPromisesInvitesAndDelete.code);
return "error";
});
})
.catch(() => {
console.log("no open invitations");
deleteGroup(groupId);
});
}
F)deleteGroup函数
function deleteGroup(groupId) {
return admin.firestore().collection('groups').doc(groupId).delete();
}
我是编程的新手,尤其是Firebase Functions,因此可以提供任何帮助!
谢谢!
答案 0 :(得分:2)
您并未在应有的位置使用return
关键字。如果您执行async
任务,则必须以某种方式“ return
”。
一些例子:
示例A:在return
之前添加Promise.all(promises)
... B:在return
之前添加Promise.all(promisesUserData)
... C:在return
之前添加checkForOthers(groupId)
... D:在return
之前添加checkForInvitesAndDelete(groupId)
... E:在return
和Promise.all(promisesInvitesAndDelete)
之前添加deleteGroup(groupId)
答案 1 :(得分:0)
我添加了“ return”语句,这很有帮助,但这并不是完整的答案。
在(D)中,我认为如果我的馆藏没有数据,它将进入“捕获”阶段,但事实并非如此。因此,我需要在“然后”阶段检查空结果集。
if (groupUsers.empty) {
return checkForInvitesAndDelete(groupId);
} else {
return "other users exist - group should not be deleted";
}
没有打开的邀请时,与(E)功能相同。