首页 > 解决方案 > 带有 where in 子句的 Firebase 函数

问题描述

根据文档,我在 Cloud Function 中有这段代码,当数组大小小于 10 时,它运行得很好。但是,如果集合超过 10,我将无法运行我的代码。这是下面的 2 个代码片段,以及我获得快照后的执行情况。

if (offline.length > 0) {
    console.log("[TAG =>", messageDocId, "] The array of offline users array length is", offline.length);
    return admin.firestore().collectionGroup('devices').where('uid', 'in', offline).get();
}
return null;

所以我将上面的数组修改为 10 个块,然后使用 promise 来获取所有块的数据,就像这样。

if (offline.length > 0) {
    console.log("[TAG =>", roomName, "->-",messageDocId,"] The array of offline users array length is =", offline.length);
    var i,j,temparray,chunk = 10;
    for (i=0,j=offline.length; i<j; i+=chunk) {
        temparray = offline.slice(i,i+chunk);
        console.log("[TAG =>", roomName, "->-",messageDocId,"] Priniting Chunks", temparray);
        offlinePromises.push(admin.firestore().collectionGroup('devices').where('uid', '==', offline[i]).get());
    }
    console.log("[TAG =>", roomName, "->-",messageDocId,"] starting promise execution");
        return Promise.all(offlinePromises);
    }

上面两个代码片段中的任何一个下面的代码库也在下面。

.then(snapshot => {
            if(snapshot === null){
                console.log("[TAG =>", roomName, "->-",messageDocId,"] The snapshot is null");  
            }
            if (offline === null || offline.length === 0 || snapshot === null) {
                console.log("[TAG =>", roomName, "->-",messageDocId,"] The array of offline users is null");
                return null;
            }
            snapshot.forEach(doc => {
                try {
                    token = doc.data().rt;
                    outputTokenList.push(token);
                } catch (tokenfetcherror) {
                    console.log("[TAG =>", roomName, "->-",messageDocId,"] unable to fetch tokens for =>", doc.id);
                }
            });
            return outputTokenList;

        }).then(response => { // prepare the payload and a batch of upto 500 and send push messages.
            if(response === null){
                return null;
            }
            console.log("[TAG =>", roomName, "->-",messageDocId,"] Successfully prepared token list ", response);

            var tokenChunks = [];

            while (response.length) {
                tokenChunks.push(response.splice(0, 400));
            }

            for (batchTokens of tokenChunks) {
                console.log("[TAG =>", roomName, "->-",messageDocId,"] Chunked token list ", batchTokens);
                let promise = admin.messaging().sendToDevice(batchTokens, payload, options);
                promises.push(promise);
            }
            return Promise.all(promises);
        })
        .then(batchResponse => { // you can handle removal candidates using this.
            if(batchResponse === null){
                console.log("[TAG =>", roomName, "->-",messageDocId,"] No Messages to send.");
                return false;
            }
            console.log("[TAG =>", roomName, "->-",messageDocId,"] Messages Sent, handle removals later");
            console.log("[TAG =>", roomName, "->-",messageDocId,"] batchResponse =>", batchResponse);
            return true;
        }).catch(error => {
            console.log("[TAG =>", roomName, "->-",messageDocId,"] Error sending message ", error);
            return false;
        })
});

我写 Promise 的方式有问题吗?请协助。TIA

标签: javascriptfirebasegoogle-cloud-firestore

解决方案


推荐阅读