Issue with programming a firebase cloud function which deletes documents in a collection if certain conditions are met

huangapple go评论74阅读模式
英文:

Issue with programming a firebase cloud function which deletes documents in a collection if certain conditions are met

问题

我有一个名为"ProductDatabase"的集合,其中存储了我在应用程序中展示的所有产品。集合中的每个文档都有一个名为"SBD"的时间戳字段,表示每个物品的销售日期。我已经创建并上传了一个云函数,每隔2小时检查数据库中的每个物品,以确保其"SBD"字段未过期。以下是该函数的代码:

const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();

exports.delete = functions
    .region("europe-west2")
    .pubsub.schedule("every 2 hours").onRun((context) => {
      const productsRef = admin.firestore().collection("TestDatabase");

      const query = productsRef.where("SBD", "<", Date.now());

      return query.get()
          .then((querySnapshot) => {
            const batch = admin.firestore().batch();

            querySnapshot.forEach((doc) => {
              batch.delete(doc.ref);
            });

            return batch.commit();
          });
    });

尽管在 Firebase 云函数控制台中显示此程序每隔2小时被调用,但是未能从"TestDatabase"中删除任何产品,尽管它们都应该被删除。"index.js"文件存储在 Google 云控制台的存储桶中,具有所有必要的权限。数据库和云函数也位于相同的区域。

英文:

I have a ProductDatabase collection which stores all the products I'm displaying on my app. Each document in the collection has a timestamp field called 'SBD' which represents the sell by date of each item. I have created and uploaded a cloud function which checks the each item in the database every 2 hours to check that every items SBD field has not passed. This is the function:

const functions = require(&quot;firebase-functions&quot;);
const admin = require(&quot;firebase-admin&quot;);
admin.initializeApp();

exports.delete = functions
    .region(&quot;europe-west2&quot;)
    .pubsub.schedule(&quot;every 2 hours&quot;).onRun((context) =&gt; {
      const productsRef = admin.firestore().collection(&quot;TestDatabase&quot;);

      const query = productsRef.where(&quot;SBD&quot;, &quot;&lt;&quot;, Date.now());

      return query.get()
          .then((querySnapshot) =&gt; {
            const batch = admin.firestore().batch();

            querySnapshot.forEach((doc) =&gt; {
              batch.delete(doc.ref);
            });

            return batch.commit();
          });
    });

This program uploads appears in the firebase cloud functions console and says it's being invoked every 2 hours however no products are deleted from the TestDatabase even though they should all be deleted. The index.js file is stored in a bucket in the google cloud console which has all the necessary permissions. The database and cloud function are also in the same region.

答案1

得分: 0

The problem is that you're doing an invalid comparison on the SBD field. If SBD is a Timestamp object then your query must use a Timestamp object in the comparison.

This is an invalid comparison and will always return 0 records because you cannot compare two different object types:

const query = productsRef.where("SBD", "<", Date.now());

This is a valid comparison performed by getting UNIX time and converting it to a Timestamp:

const seconds = Math.floor(Date.now() / 1000)
const nanoseconds = 0
const now = new Timestamp(seconds, nanoseconds)
const query = productsRef.where("SBD", "<", now);

You can read more at How to compare firebase timestamps?.

英文:

The problem is that you're doing an invalid comparison on the SBD field. If SBD is a Timestamp object then your query must use a Timestamp object in the comparison.

This is an invalid comparison and will always return 0 records because you cannot compare two different object types:

const query = productsRef.where(&quot;SBD&quot;, &quot;&lt;&quot;, Date.now());

This is a valid comparison performed by getting UNIX time and converting it to a Timestamp:

const seconds = Math.floor(Date.now() / 1000)
const nanoseconds = 0
const now = new Timestamp(seconds, nanoseconds)
const query = productsRef.where(&quot;SBD&quot;, &quot;&lt;&quot;, now);

You can read more at How to compare firebase timestamps?.

huangapple
  • 本文由 发表于 2023年4月7日 00:49:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75951926.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定