繁体   English   中英

Firestore 如何制作新的时间戳字段以及如何查询日期?

[英]Firestore how to make new timestamp field and how to query with dates?

exports.resetDailyFinalKills = functions.pubsub
    .schedule("*/5 * * * *")
    .timeZone("Europe/Berlin")
    .onRun(async (context) => {
      const players = firestore.collection("players");
      const goodtimes = await players
          .where("registerDate", "<=", Date.now()-84600)
          .get();
      goodtimes.docs.forEach((snapshot) => {
        const uuid = snapshot.data().uuid;
        const url = `https://api.hypixel.net/player?key=x&uuid=${uuid}`;
        const settings = {method: "Get"};

        fetch(url, settings)
            .then((res) => res.json())
            .then((data) => {
              const finals = data.player.stats.Bedwars.final_kills_bedwars;
              const date = snapshot.data().registerDate;
              snapshot.ref.update({todayFinalKills: finals});
              snapshot.ref.update({registerDate: firebase.firestore
                  .Timestamp.fromDate(new Date(date+84600))});
              console.log("got in");
              console.log(finals);
            });
        //  snapshot.ref.update({final_kills: 0});
      });
      return null;
    });

所以我有这个function,第一个问题是

snapshot.ref.update({registerDate: firebase.firestore
                      .Timestamp.fromDate(new Date(date+84600))});

我正在尝试向 registerDate 字段添加新的时间戳,但它不起作用? Second.where("registerDate", "<=", Date.now()-84600) 在这里我正在尝试查找超过 24 小时的日期,但这也不起作用,因为我猜日期不是简单地存储为数字但我不确定我会怎么做我想做的事?

时间戳不存储为数字。 使用数字作为过滤器的查询永远不会匹配时间戳,就像您在这里所做的那样:

const goodtimes = await players
          .where("registerDate", "<=", Date.now()-84600)
          .get();

该查询将仅匹配 registerDate 是请求范围内的数字的文档。

如果要将文档字段与时间戳匹配,则必须在查询过滤器中使用时间戳 object 日期也可以。

const goodtimes = await players
          .where("registerDate", "<=", new Date(Date.now()-84600))
          .get();

暂无
暂无

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

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