Firebase的Cloud Functions返回对GCF的承诺数组

时间:2017-04-05 22:58:09

标签: firebase firebase-realtime-database google-cloud-functions

在我的onWrite事件处理程序中,我在不同的数据库路径中执行数据更新。无需完成任务即可中断该功能。我正在实施Promise.all()向GCF发送一系列承诺,并等待所有未完成的工作/任务完成。控制台日志未显示错误。我试图找出我是否正在做正确的实施。

    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);
});

1 个答案:

答案 0 :(得分:2)

Bellow功能正常工作,Promise数组被发送到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);
});