firebase云功能更改事件时间戳错误



我们正在建立一个系统,其中firestore的变化产生了Pubsub-Message,其他组件可以在其中作用。

Firestore事件用于实现此目标。该系统将每个文档的更改历史记录为文档的子集合。为了减少pubsub-messages的大小,我们仅在此子集合中向文档发送参考。更改之前和一个。为了允许我们的功能不浪费时间来弄清楚最后一个状态是我们在更改事件上使用的时间戳作为ID。

env.ts:

import * as functions from 'firebase-functions';
import {handler} from '@handlers/rootCollection';
//noinspection JSUnusedGlobalSymbols
export const handlerFunction = functions.firestore
    .document('rootcollection/{documentID}')
    .onWrite(handler.process.bind(handler));

rootCollection.ts:

import {DocumentSnapshot} from 'firebase-functions/lib/providers/firestore';
import {Change, EventContext} from 'firebase-functions';
interface MessageI extends Object {
    eventID: string;
    ref: string;
    beforeID: string | null;
    afterID: string | null;
    fcfReceived: number | null;
}
class WriteHandler {
    public name: string;
    public constructor(name: string) {
        this.name = name;
    }
    public async process(change: Change<DocumentSnapshot>, context: EventContext): MessageI {
        const afterExists = change.after.exists;
        const beforeExists = change.before.exists;
        const afterID = afterExists ? change.after.updateTime.toMillis().toString() : null;
        const beforeID = beforeExists ? change.before.updateTime.toMillis().toString() : null;
        return {
            'eventID': context.eventId,
            'ref': change.before.ref.path,
            'beforeID': beforeID,
            'afterID': afterID,
            'fcfReceived': Date.now()
        };
    }
}
export const itinerariesHandler = new WriteHandler(
    'rootCollectionHandler'
);

现在,我们很惊讶数据正确地反映了之前和之后的状态,但是时间戳似乎是after.timestamp。

发布的消息429345402459000到主题。消息:

{
  "eventID":    "389fe158-115a-43ca-9fdd-ec9737af066f-0",
  "ref":        "collection/VMDcsHirbB2coyaXF5wZ",
  "beforeID":   "1552298315479",
  "afterID":    "1552298315479",
  "fcfReceived": 1552298315575,
}

change.before.updateTime包含完成更改的时间似乎是违反直觉的。但是我找不到文档中的明确提及。

这种行为是打算的吗?有其他方法可以找到前面状态的唯一ID吗?

问候,Carsten

firebaser在这里

这是云功能的Firestore绑定中的错误。它确实将before.updateTimeafter.updateTime设置为相同的值。

我们在内部提交了一个错误以解决此问题。但是没有时间表可以使用此修复程序。

直到那时,我能想到的最好的是在文档数据中添加lastUpdated字段,并验证您的安全规则中当前的服务器端时间戳。这样,您可以使before.data().lastUpdatedafter.data().lastUpdated进行比较。这至少应该允许您实施该行为,直到解决问题为止。

最新更新