首页 > 解决方案 > Firestore + Cloud Functions:日期行为已更改,您的应用程序可能会中断

问题描述

我的所有由 Firestore 后台触发器调用的 Cloud Functions 都出现以下错误。

我不确定为什么会弹出错误。客户端是 Swift (iOS),我使用 ISO8601 字符串作为日期(不是日期对象)。

根据上一个问题: Firebase Cloud Functions - Change Firestore settings

@Doug Stevensen 说:

从快照中读取日期时,您应该使用 Timestamp 对象。如果您这样做,您可以忽略此警告消息。

他从快照中读取日期是什么意思?当我阅读任何日期时,我使用客户端创建的 ISO 8601 字符串(存储为我的文档数据的一部分)。我从不使用/参考context.timestamp,所以我应该能够忽略这些警告吗?

有什么办法可以使这个警告静音,这样它就不会在每次运行云函数时出现?或者我可以忽略警告吗?

The behavior for Date objects stored in Firestore is going to change
    AND YOUR APP MAY BREAK.
    To hide this warning and ensure your app does not break, you need to add the
    following code to your app before calling any other Cloud Firestore methods:

  const firestore = new Firestore();
  const settings = {/* your settings... */ timestampsInSnapshots: true};
  firestore.settings(settings);

With this change, timestamps stored in Cloud Firestore will be read back as
Firebase Timestamp objects instead of as system Date objects. So you will also
need to update code expecting a Date to instead expect a Timestamp. For example:

  // Old:
  const date = snapshot.get('created_at');
  // New:
  const timestamp = snapshot.get('created_at');
  const date = timestamp.toDate();

Please audit all existing usages of Date when you enable the new behavior. In a
future release, the behavior will change to the new behavior, so if you do not
follow these steps, YOUR APP MAY BREAK.

标签: google-cloud-firestoregoogle-cloud-functions

解决方案


推荐阅读