将Firestore的时间戳与NodeJS的时间戳在Google Cloud函数中进行比较。

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

Compare Firestore timestamp with NodeJS timestamp in google cloud function

问题

我正在尝试比较时间戳以触发云函数中的if语句

当前我收到一个错误即toDate不是一个函数根据我的理解这是由于数据的序列化造成的请帮忙解决如何将其转回对象

英文:

I am trying to compare timestamp to trigger a if statement in my cloud function.

    const orderRef = await admin
    .firestore()
    .collection('orders')
    .doc(orderId)
    .get();

    const createdAt = orderRef.data()!.createdAt.toDate();
    const currentTime = new Date();
    
    // Calculate difference
    const diff = currentTime.getTime() - createdAt; 
    
    //convert difference to minutes
    const diffInMinutes = Math.floor(diff / 60000);
    console.log('Diff in minutes: ' + diffInMinutes);
    
    // Main Logic
    if (diffInMinutes <= 10) {
      //Logic
    }

Right now, I am getting error that is toDate is not a function and as per my understanding that is due to serialisation of data. Kindly help with how can I convert it back to object.

答案1

得分: 2

正如 @Frank van Puffelen 上面提到的那样。有可能您的 createdAt 字段根本不是一个 Timestamp。为此,您可以添加一个条件语句来进行检查,如下所示:

import {
    Timestamp
} from "firebase-admin/firestore";
const firestore = admin.firestore();
const orderRef = await firestore.collection("orders").doc(orderId).get();
const docData = orderRef.data();
console.log(docData);
if (docData?.createdAt instanceof Timestamp) {
    const createdAtDate = docData?.createdAt.toMillis(); // 返回自Unix纪元以来的毫秒数
    const currentTime = new Date();

    // 计算差值
    const diff = currentTime.getTime() - createdAtDate;

    // 将差值转换为分钟
    const diffInMinutes = Math.floor(diff / 60000);
    console.log("差值(分钟):" + diffInMinutes);

    // 主要逻辑
    if (diffInMinutes <= 10) {
        console.log("差值小于等于10:" + diffInMinutes);
    } else {
        console.log("差值大于10:" + diffInMinutes);
    }
} else {
    console.log(docData?.createdAt, " 不是一个 Timestamp");
}

您可以直接使用 toMillis() 方法将 Timestamp 转换为数值,而不是先将其转换为日期和时间。

参考:

英文:

As @Frank van Puffelen mentioned above. It is possible that your createdAt field is not a Timestamp at all. For that you can add a if clause to check as follows :

import {
    Timestamp
} from &quot;firebase-admin/firestore&quot;;
const firestore = admin.firestore();
const orderRef = await firestore.collection(&quot;orders&quot;).doc(orderId).get();
const docData = orderRef.data();
console.log(docData);
if (docData?.createdAt instanceof Timestamp) {
    const createdAtDate = docData?.createdAt.toMillis(); // Returns the number of milliseconds since Unix epoch
    const currentTime = new Date();

    // Calculate difference
    const diff = currentTime.getTime() - createdAtDate;

    // Convert difference to minutes
    const diffInMinutes = Math.floor(diff / 60000);
    console.log(&quot;Diff in minutes: &quot; + diffInMinutes);

    // Main Logic
    if (diffInMinutes &lt;= 10) {
        console.log(&quot;Difference is less than 10: &quot; + diffInMinutes);
    } else {
        console.log(&quot;Difference is more than 10: &quot; + diffInMinutes);
    }
} else {
    console.log(docData?.createdAt, &quot; is not a Timestamp&quot;);
}

Instead of converting Timestamp to Date and then Time you can directly convert Timestamp to a numeric value with toMillis() method

Reference :

答案2

得分: 0

生成的日期时间格式不同。

createdAt 正在使用 ".toDate()" 进行转换。

currentTime.getTime()

这两个函数将生成不同格式的输出,因此无法简单地通过 var1-var2 来获取差异。

首先将它们都转换成相同的格式,然后它将起作用。

英文:

Resulted datetime formats of both variables are different.

createdAt is converting with ".toDate()"

currentTime.getTime()

these 2 functions will generate output in different formats.due to that it is not possible to get the difference simply var1-var2.

first convert both in same format then it will work.

huangapple
  • 本文由 发表于 2023年7月3日 16:45:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76603173.html
匿名

发表评论

匿名网友

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

确定