简体   繁体   English

Firebase Cloud Function - 获取所有匹配当前时间戳的文档

[英]Firebase Cloud Function - Fetch all docs matching current Timestamp

I am running a scheduled cloud function that executes every 60 seconds.我正在运行每 60 秒执行一次的预定云 function。 Within this function I am trying to fetch all the docs that have a value 'remindAt' matching current Timestamp.now().在这个 function 中,我试图获取所有具有与当前 Timestamp.now() 匹配的值“remindAt”的文档。

After these have been fetched I would like to perform another function;在获取这些之后,我想执行另一个 function; for now it is just updating a value within the doc, however later on it will be sending a notification to the users device...现在它只是更新文档中的一个值,但是稍后它会向用户设备发送通知......

In the Google Cloud log console I am not receiving any errors, however, nothing seems to be updating in Firestone and I have noticed I am getting some random spikes of read/write documents.在 Google Cloud 日志控制台中,我没有收到任何错误,但是,Firestone 中似乎没有任何更新,我注意到我收到了一些随机的读/写文档峰值。 Even if the code was working it should not be more than 2 or 3 read/writes at the moment.即使代码正在运行,目前也不应超过 2 或 3 次读/写。

Please see node.js code below & screenshot of firebase console.请参阅下面的 node.js 代码和 firebase 控制台的屏幕截图。

This is my first time working with firebase cloud functions etc, so most likely my code is not the best... I'd appreciate any assistance!这是我第一次使用 firebase 云函数等,所以很可能我的代码不是最好的......我会很感激任何帮助!

const timestamp = admin.firestore.Timestamp.now();

exports.checkReminders = functions.pubsub.schedule("* * * * *").onRun((context) => {

      const reminderRef = database.collection("Reminders");
      reminderRef.where("remindAt", "==", timestamp).get().then((snapshot) => {

        if (snapshot.empty) {
          console.log("No matching documents.");
          return;
        }

        snapshot.forEach((doc) => {
          doc.ref.update({"completed": true});
          console.log("Updated completed to True");
        });
      });
  });

截屏

So I have managed to solve my issues above.所以我设法解决了上面的问题。

Issue 1, random spikes: This was caused from me opening Firebase Cloud Firestore Data... Basically opening/leaving the web page multiple times started to create a spike in read data...问题 1,随机尖峰:这是由于我打开 Firebase Cloud Firestore 数据引起的...基本上多次打开/离开 web 页面开始在读取数据中产生尖峰...

The code I have change it to the following: Basically I have created a cloud function to check the Reminder collection every 60 seconds for any documents that have a reminder at less than or equal to the current time.now() and if so it sets completed to true (this is so it cannot be called again in the future.)我已将其更改为以下代码:基本上,我创建了一个云 function 以每 60 秒检查一次提醒集合,以查找提醒时间小于或等于当前时间的任何文档。现在(),如果是这样,它会设置完成为真(这是为了将来不能再次调用它。)

    exports.checkReminders = functions.pubsub
        .schedule("* * * * *").onRun(async (context) => {
          const query = await database.collection("Reminders")
              .where("remindAt", "<=", admin.firestore.Timestamp.now())
              .where("completed", "==", false).get();
    
          query.forEach(async (snapshot) => { 

// Here you can call another function which sends the notification to a specific device.

            await database.doc("Reminders/" + snapshot.id).update({
              "completed": true, 
            });
          });
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM