javascript - 带有 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
解决方案
推荐阅读
- java - 对于 Scanner 类型,方法 nextInt() 未定义
- delphi - Delphi中TToolButton Caption的多行文本
- laravel - Vue js PATCH http://127.0.0.1:8000/api/auth/profile 401(未经授权)
- markdown - 在 Markdown 中格式化深层列表
- php - 匹配令牌及其可能重叠的上下文
- javascript - mapbox-gl-js 路线不准确
- rdf - 与“小”个体打交道的最佳方法是什么?
- c++ - 使用管道从 STDIN 读取分叉进程的问题
- php - 如何将数组添加到我的 php 变量中,循环不适用于数组
- javascript - 正则表达式匹配字符串的句子和单词