gpt4 book ai didi

firebase - 在 Firebase 函数中等待多个 Promise

转载 作者:行者123 更新时间:2023-12-04 01:53:48 26 4
gpt4 key购买 nike

我试图弄清楚如何在函数结束之前等待多个 promise 被执行。

本质上,这就是我尝试做的:

  • 如果用户删除他/她的帐户,我想清除与他/她相关的所有数据
  • 直接数据可以删除
  • 如果用户是组的一部分,则该组仍然存在,如果其他用户在该组中;否则该组也将被删除

  • 这是我到目前为止尝试过的:

    A) Main 函数(启动第一层 Promise):
    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 函数,所以任何帮助将不胜感激!!

    谢谢!

    最佳答案

    您没有使用 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)

    关于firebase - 在 Firebase 函数中等待多个 Promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51749688/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com