Firestore游标不返回任何文档

时间:2019-10-28 11:47:45

标签: javascript firebase google-cloud-firestore

我已经使用Firestore触发器提供了队列服务,但是在使光标工作时遇到了一些问题。

每5分钟将触发以下功能。它检查队列收集文档是否设置为正在运行,如果没有,它将获取第一个文档,并从该文档中获取id字段并更新队列收集文档

exports.scheduledFunction = functions.pubsub.schedule('every 5 minutes').onRun(async(context) => {
    try{
        const q = await admin.firestore().collection('queueService').doc('extension').get();
        const status = q.data().status;

        if(status !== "start" || status !== "continue) {
            const first = admin.firestore().collectionGroup('extItems')
                .where('platform', "==", "mba")
                .orderBy('created_at')
                .limit(1);
            const snapshot = await first.get();
            await firestoreUpdateCursor(snapshot.docs[0].data().id, 1, "start", "extension");
        } else {
            console.log('Sync is currently running')
        }
    } catch (e) {
        console.log(e.message)
    }
});

下面的函数然后在更新queueService集合文档时触发。我要执行的操作是使用startAt光标。然后,将按以下方式获取文档,并对其进行X处理,完成后,它将使用最后一个ID更新queueService,这将再次触发该函数,直到没有剩余的项目为止。

module.exports = functions.firestore.document("queueService/extension").onUpdate(async(change, context) => {
    try {
        const {cursor, lastCount, status} = change.after.data();
        const iPromises = [];
        const fullfilled = [];

        let next;
        if(lastCount > 0) {
            if(status === "start" || status === "continue") {
                next = admin.firestore().collectionGroup('extItems')
                    .where('platform', "==", "mba")
                    .orderBy('created_at')
                    .startAt(cursor)
                    .limit(100);
            } else {
                console.log("Extension Items already processing");
                return null;
            }

            const result = await next.get();
            console.log(result.docs.length);
            if(result.docs.length <= 0) return await firestoreUpdateCursor(null, result.docs.length, null, "extension");
            //do more stuff
            await firestoreUpdateCursor(result.docs[result.docs.length - 1].data().id, result.docs.length, "continue", "extension")
      }

    } catch (e) {
        console.log(e.message)
    }
});

我遇到的问题是我的光标不起作用。预定函数将queueService更新为正常,但随后触发的函数不返回任何文档

0 个答案:

没有答案