Cloud Functions for Firebase return 对 GCF 的承诺数组

Cloud Functions for Firebase return array of promises to GCF

在我的 onWrite 事件处理程序中,我在不同的数据库路径中执行数据更新。该功能在未完成执行任务的情况下被中断。我正在实施 Promise.all() 以向 GCF 发送承诺数组并等待所有未完成的 work/tasks 完成。控制台日志未显示错误。我想知道我是否在做正确的实施。

    exports.ObserveProposals = functions.database.ref("/proposals/{jobid}/{propid}").onWrite((event) => {
        const jobid = event.params.jobid;
        const userId = event.params.propid;
        const promises = [];
        const isinvitation = event.data.child("isinvitation").val();

        if (!isinvitation) {
            return userRef = admin.database().ref(`users/${userId}/proposals/sent`);
            if (event.data.exists() && !event.data.previous.exists()) {
                return userRef.child(jobid).set({
                    timestamp: admin.database.ServerValue.TIMESTAMP
                });
            } else if (!event.data.exists() && event.data.previous.exists()) {
                return userRef.child(jobid).remove();
            }
        promises.push(userRef);
        }
        return collectionRef = admin.database().ref(`/jobs/${jobid}`).once('value').then(snapshot => {
        if (snapshot.val() !== null) {
          return countRef = collectionRef.child("proposals").transaction(current => {
            if (event.data.exists() && !event.data.previous.exists()) {
              return (current || 0) + 1;
            } else if (!event.data.exists() && event.data.previous.exists()) {
              return (current || 0) - 1;
            }
          });
         }
         promises.push(collectionRef);
       });
       return Promise.all(promises);
});

Bellow 函数正常工作,promises 数组正在发送到 GCF,所有任务都已执行。

exports.ObserveProposals = functions.database.ref("/proposals/{jobid}/{propid}").onWrite((event) => {
    const jobid = event.params.jobid;
    const userid = event.params.propid;
    const promises = [];
    let userRef = admin.database().ref(`users/${userid}/proposals`);
    let jobRef = admin.database().ref(`/jobs/${jobid}/proposals`);
    jobRef.once('value').then(snapshot => {
        if (snapshot.val() !== null) {
            jobRef.transaction(current => {
                if (event.data.exists() && !event.data.previous.exists()) {
                    return (current || 0) + 1;
                } else if (!event.data.exists() && event.data.previous.exists()) {
                    return (current || 0) - 1;
                }
            });
        }
    });
    promises.push(jobRef);
    if (event.data.exists() && !event.data.previous.exists()) {
        const isInvitation = event.data.child("isinvitation").val();
        if (!isInvitation) {
            return userRef.child(`/sent/${jobid}`).set({
                timestamp: admin.database.ServerValue.TIMESTAMP
            });
        } else if (isInvitation) {
            return userRef.child(`/received/${jobid}`).set({
                timestamp: admin.database.ServerValue.TIMESTAMP
            });
        }
    } else if (!event.data.exists() && event.data.previous.exists()) {
        const isInvitation = event.data.previous.child("isinvitation").val();
        if (!isInvitation) {
            return userRef.child(`/sent/${jobid}`).remove();
        }else if (isInvitation) {
            return userRef.child(`/received/${jobid}`).remove();
        }
    }
    promises.push(userRef);
    return Promise.all(promises);
});