首页 > 解决方案 > Firebase Cloud Functions onUpdate 触发器向 Firestore 返回不可靠的时间戳

问题描述

我在 Cloud Functions 中有一个简单的函数,当我的 Cloud Firestore 数据库中的“客户端”位置发生任何更新时会触发该函数。

问题是,当我更新数据时,一旦将文档的 lastUpdated 子项更改为秒 1572841408099,然后我再次更改子项,它总是将 lastUpdated 恢复为当时的 Cloud Firestore 时间戳:“2019 年 11 月 3 日UTC-6 晚上 9:44:16”,无论它实际上是什么时间。

如何获得以下代码以可靠地返回数据的更新时间?

import * as functions from 'firebase-functions';

export const clientUpdatedTask = functions.firestore.document('clients/{clientId}').onUpdate((change: any, context: any) => {
    const before = change.before.data();
    const after = change.after.data();
    const timeEdited = Date.now();
    if(before.lastUpdated.seconds !== after.lastUpdated.seconds) {
        return 'The Time Did Not Change. No Need To Run Update!';
    } else {
        return change.after.ref.update({'lastUpdated' : timeEdited}, {merge: true});
    }
});

为什么它会在第二次暗示后恢复到过去一段时间的常规时间戳?此外,为什么每次更新时它会在秒数和时间戳之间跳转?

标签: javascriptfirebasegoogle-cloud-firestoregoogle-cloud-functions

解决方案


推荐阅读