我们正在构建一个系统,在这个系统中,Firestore中的更改会产生PubSub消息,其他组件可以在其中工作。
萤火虫事件用于实现这一点。系统将每个文档的更改历史记录作为文档的子集合。为了减少PubSub消息的大小,我们只发送对这个子集合中的文档的引用。一次在改变之前,一次在改变之后。为了不让我们的函数在查询上浪费时间,找出最后一个状态是什么,我们将更改事件上的时间戳作为ids使用。
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?
你好,卡斯滕
发布于 2019-03-25 16:28:26
这里的消防队员
这是云函数的修复绑定中的错误。它确实将before.updateTime和after.updateTime设置为相同的值。
为了解决这个问题,我们在内部注册了一个bug。但是没有这个修复程序何时可用的时间表。
在此之前,我能想到的最好方法是向文档数据中添加一个lastUpdated字段,并验证这是您的安全规则中当前服务器端的时间戳。这样,您就可以让before.data().lastUpdated和after.data().lastUpdated进行比较。这至少应该允许您实现行为,直到问题得到解决为止。
https://stackoverflow.com/questions/55100854
复制相似问题