首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Firebase云功能更改事件时间戳错误

问Firebase云功能更改事件时间戳错误
EN

Stack Overflow用户
提问于 2019-03-11 11:28:03
回答 1查看 657关注 0票数 2

我们正在构建一个系统,在这个系统中,Firestore中的更改会产生PubSub消息,其他组件可以在其中工作。

萤火虫事件用于实现这一点。系统将每个文档的更改历史记录作为文档的子集合。为了减少PubSub消息的大小,我们只发送对这个子集合中的文档的引用。一次在改变之前,一次在改变之后。为了不让我们的函数在查询上浪费时间,找出最后一个状态是什么,我们将更改事件上的时间戳作为ids使用。

env.ts:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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。消息:

代码语言:javascript
复制
{
  "eventID":    "389fe158-115a-43ca-9fdd-ec9737af066f-0",
  "ref":        "collection/VMDcsHirbB2coyaXF5wZ",
  "beforeID":   "1552298315479",
  "afterID":    "1552298315479",
  "fcfReceived": 1552298315575,
}

change.before.updateTime包含了完成更改的时间,这似乎有悖常理。但我在文件中找不到明确的内容。

这种行为是故意的吗?是否有另一种方法可以为以前的状态找到唯一的id?

你好,卡斯滕

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-25 16:28:26

这里的消防队员

这是云函数的修复绑定中的错误。它确实将before.updateTime和after.updateTime设置为相同的值。

为了解决这个问题,我们在内部注册了一个bug。但是没有这个修复程序何时可用的时间表。

在此之前,我能想到的最好方法是向文档数据中添加一个lastUpdated字段,并验证这是您的安全规则中当前服务器端的时间戳。这样,您就可以让before.data().lastUpdated和after.data().lastUpdated进行比较。这至少应该允许您实现行为,直到问题得到解决为止。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55100854

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档