如何在云功能中从Firestore访问多个文档

时间:2019-01-07 21:53:37

标签: javascript angular firebase google-cloud-firestore google-cloud-functions

我具有在写入文档时触发的云功能。云功能需要根据触发器检查多个文档并执行if / else语句。

我创建了一个函数,该函数可以使用Promise.all访问所有文档,但是在尝试访问所有文档信息(如果尚不可用)时出现此错误。

    export function onTriggered(change, context) {
        const userPrevData = change.before.data();
        const userNewData  = change.after.data();

        const promises = [];

        // Get the uid
        const uid = context.params.userId;
        // User DocRef
        const userDoc        = firestoreInstance.collection('users').doc(uid).get();
        // User Session DocRef
        const userSessionDoc = firestoreInstance.collection('sessions').doc(uid).get();
        // Solution DocRef
        const solutionDoc    = firestoreInstance.collection('solution').doc('solutionId').get();

        promises.push(userDoc, userSessionDoc, solutionDoc);

        return Promise.all(promises).then((snapshots) => {
            // Use Promise.all with snapshot.docs.map to combine+return Promise context
            return Promise.all(snapshots.map((doc) => {
                // At this point, each document and their Ids print to the console.
                console.log('docId:::', doc.id);
                console.log('docData:::', doc.data());

                const solutionDocData = getSolutionDocData(doc);
                // This will print as 'undefined' until the correct document data is processed
                console.log('solutionDocData:::', solutionDocData);
                // This will print as 'undefined' until the correct document data is processed
                const clientSeed = doc.get('clientSeed');

                // Check to see if the Users' Guess is the same as the solution
                if (userNewData.guess.color === solutionDocData.guess.color && userNewData.guess.number === userNewData.guess.number) {
                    console.log('User solution is correct');
                }

            }));

        })
    }

    function getSolutionDocData(doc) {
        if (doc.id === 'solutionId') { return doc.data(); }
    }

如果满足条件,我希望“用户解决方案正确”。但是,我得到一个错误,因为数据是不确定的。

1 个答案:

答案 0 :(得分:1)

解决方案是将大部分逻辑移至.then()

    return Promise.all(promises).then((snapshots) => {
        // Use Promise.all with snapshot.docs.map to combine+return Promise context
        return Promise.all(snapshots.map((doc) => {
            // At this point, each document and their Ids print to the console.
            console.log('docId:::', doc.id);
            console.log('docData:::', doc.data());

            return doc.data();
        })).then(data => { 
        console.log('data:::', data);

        let userDocDetails = {};
        let userSessionDocDetails = {};
        let solutionDocDetails = {};

        data.forEach(document => {
            if (document.uid === uid)           { userDocDetails = document }
            if (document.serverSeedEncrypted)   { userSessionDocDetails = document }
            if (document.solutionId)            { solutionDocDetails = document }
        });


        });

    })

我不确定数据是否总是按照原始的Promise数组的顺序返回,因此我使用了forEach语句来标识唯一属性并相应地分配它们。