Firestore 转向 Firebase 函数中的时间戳
Firestore moving to timestamps in Firebase functions
昨天,我所有的 firebase 函数都开始抛出以下警告:
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.
现在我想根据这个警告正确地初始化我的 firestore。我正在使用打字稿。
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp(functions.config().firebase);
let fireStoreDB = fireStoreDB || admin.firestore()
admin return 的 firestore() 没有警告中描述的 .settings() 方法,也没有在构造函数中获取对象。 (它得到一个 App 对象)。
所以我有两个问题:
如何初始化我的 firestore 以获取设置对象?
当我向文档插入日期或查询文档时是否还需要传递 Firestore.Timestamp 对象?或者我可以 query/insert 普通的 JS Date 对象,它会自动转换吗?
谢谢。
编辑:
我设法使用以下方法解决了 http 函数的问题:
if (!fireStoreDB){
fireStoreDB = admin.firestore();
fireStoreDB.settings(settings);
}
但它仍然在 firestore 触发器上发生。
任何人都知道如何在 :
上为管理员提供默认设置
admin.initializeApp(functions.config().firebase);
所以它不会发生在 firestore 触发器上?
由于错误,您仍然收到 JS 日期而不是 Firestore 时间戳...现已在 Firebase Functions v2.0.2 中修复。
参见:https://github.com/firebase/firebase-functions/releases/tag/v2.0.2.
对于初始化,我使用了警告消息中指定的 admin.firestore().settings({timestampsInSnapshots: true})
,因此警告已消失。
当您向 Document 添加日期,或用作查询参数时,您可以使用普通的 JS Date 对象。
在您的 index.js firebase 函数文件中添加以下第二行代码
admin.initializeApp(functions.config().firebase);
admin.firestore().settings( { timestampsInSnapshots: true })
通过以下方式解决:
const settings = {timestampsInSnapshots: true};
if (!fireStoreDB){
fireStoreDB = admin.firestore();
fireStoreDB.settings(settings);
}
对于firebase-admin
版本7.0.0或以上
您应该不会再收到此错误。
在Version 7.0.0 - January 31, 2019 of firebase-admin
there were some breaking changes中介绍:
BREAKING: The timestampsInSnapshots
default has changed to true.
The timestampsInSnapshots
setting is now enabled by default so timestamp
fields read from a DocumentSnapshot
will be returned as Timestamp
objects
instead of Date
. Any code expecting to receive a Date
object must be
updated.
注意: 如 the official docs 所述,timestampsInSnapshots
将在未来的版本中删除,因此请确保将其完全删除。
旧版本 firebase-admin
(6.5.1 及以下)
这应该可以完成工作:
const admin = require('firebase-admin');
admin.initializeApp();
const firestore = admin.firestore();
// Add this magical line of code:
firestore.settings({ timestampsInSnapshots: true });
然后在你的函数中直接使用 firestore 对象:
firestore.doc(`/mycollection/${id}`).set({ it: 'works' })
昨天,我所有的 firebase 函数都开始抛出以下警告:
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.
现在我想根据这个警告正确地初始化我的 firestore。我正在使用打字稿。
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp(functions.config().firebase);
let fireStoreDB = fireStoreDB || admin.firestore()
admin return 的 firestore() 没有警告中描述的 .settings() 方法,也没有在构造函数中获取对象。 (它得到一个 App 对象)。 所以我有两个问题:
如何初始化我的 firestore 以获取设置对象?
当我向文档插入日期或查询文档时是否还需要传递 Firestore.Timestamp 对象?或者我可以 query/insert 普通的 JS Date 对象,它会自动转换吗?
谢谢。
编辑:
我设法使用以下方法解决了 http 函数的问题:
if (!fireStoreDB){
fireStoreDB = admin.firestore();
fireStoreDB.settings(settings);
}
但它仍然在 firestore 触发器上发生。 任何人都知道如何在 :
上为管理员提供默认设置admin.initializeApp(functions.config().firebase);
所以它不会发生在 firestore 触发器上?
由于错误,您仍然收到 JS 日期而不是 Firestore 时间戳...现已在 Firebase Functions v2.0.2 中修复。 参见:https://github.com/firebase/firebase-functions/releases/tag/v2.0.2.
对于初始化,我使用了警告消息中指定的 admin.firestore().settings({timestampsInSnapshots: true})
,因此警告已消失。
当您向 Document 添加日期,或用作查询参数时,您可以使用普通的 JS Date 对象。
在您的 index.js firebase 函数文件中添加以下第二行代码
admin.initializeApp(functions.config().firebase);
admin.firestore().settings( { timestampsInSnapshots: true })
通过以下方式解决:
const settings = {timestampsInSnapshots: true};
if (!fireStoreDB){
fireStoreDB = admin.firestore();
fireStoreDB.settings(settings);
}
对于firebase-admin
版本7.0.0或以上
您应该不会再收到此错误。
在Version 7.0.0 - January 31, 2019 of firebase-admin
there were some breaking changes中介绍:
BREAKING: The
timestampsInSnapshots
default has changed to true.The
timestampsInSnapshots
setting is now enabled by default so timestamp fields read from aDocumentSnapshot
will be returned asTimestamp
objects instead ofDate
. Any code expecting to receive aDate
object must be updated.
注意: 如 the official docs 所述,timestampsInSnapshots
将在未来的版本中删除,因此请确保将其完全删除。
旧版本 firebase-admin
(6.5.1 及以下)
这应该可以完成工作:
const admin = require('firebase-admin');
admin.initializeApp();
const firestore = admin.firestore();
// Add this magical line of code:
firestore.settings({ timestampsInSnapshots: true });
然后在你的函数中直接使用 firestore 对象:
firestore.doc(`/mycollection/${id}`).set({ it: 'works' })