英文:
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 "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(); // 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("Diff in minutes: " + diffInMinutes);
// Main Logic
if (diffInMinutes <= 10) {
console.log("Difference is less than 10: " + diffInMinutes);
} else {
console.log("Difference is more than 10: " + diffInMinutes);
}
} else {
console.log(docData?.createdAt, " is not a Timestamp");
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论