Sha*_*der 10 timestamp firebase google-cloud-functions firebase-admin google-cloud-firestore
昨天,我所有的firebase函数都开始抛出以下警告:
存储在Firestore中的Date对象的行为将发生变化,您的应用程序可能会发生变化.要隐藏此警告并确保您的应用不会中断,您需要在调用任何其他Cloud Firestore方法之前将以下代码添加到您的应用中:
Run Code Online (Sandbox Code Playgroud)const firestore = new Firestore(); const settings = {/* your settings... */ timestampsInSnapshots: true}; firestore.settings(settings);通过此更改,存储在Cloud Firestore中的时间戳将作为Firebase Timestamp对象而不是系统Date对象读回.因此,您还需要更新期望日期的代码,而不是期望时间戳.例如:
Run Code Online (Sandbox Code Playgroud)// Old: const date = snapshot.get('created_at'); // New: const timestamp = snapshot.get('created_at'); const date = timestamp.toDate();启用新行为时,请审核Date的所有现有用法.在将来的版本中,行为将更改为新行为,因此如果您不按照这些步骤操作,您的应用程序可能会突然发生.
现在我想根据此警告正确初始化我的firestore.我正在使用打字稿.
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp(functions.config().firebase);
let fireStoreDB = fireStoreDB || admin.firestore()
Run Code Online (Sandbox Code Playgroud)
从admin返回的firestore()没有警告中描述的.settings()方法,也没有在构造函数中获取对象.(它获取一个App对象).所以我有两个问题:
如何初始化我的firestore来获取设置对象?
当我向文档插入日期或当我查询文档时,我还需要传递Firestore.Timestamp对象吗?或者我可以查询/插入正常的JS Date对象,它会自动转换吗?
谢谢.
编辑:
我设法使用以下方法为http函数解决它:
if (!fireStoreDB){
fireStoreDB = admin.firestore();
fireStoreDB.settings(settings);
}
Run Code Online (Sandbox Code Playgroud)
但它仍然发生在firestore触发器上.任何人都知道如何给管理员提供默认设置:
admin.initializeApp(functions.config().firebase);
Run Code Online (Sandbox Code Playgroud)
所以它不会发生在firestore触发器上?
mar*_*amy 12
由于存在错误,您仍然收到JS Date而不是Firestore Timestamp ...现在已在Firebase Functions v2.0.2中修复.请参阅:https://github.com/firebase/firebase-functions/releases/tag/v2.0.2.
对于初始化,我已admin.firestore().settings({timestampsInSnapshots: true})按警告消息中的指定使用,因此警告已消失.
向Document添加日期或用作查询参数时,可以使用普通的JS Date对象.
| 归档时间: |
|
| 查看次数: |
6014 次 |
| 最近记录: |