Firestore移至Firebase函数中的时间戳记 [英] Firestore moving to timestamps in Firebase functions

查看:57
本文介绍了Firestore移至Firebase函数中的时间戳记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天,我所有的firebase函数开始引发以下警告:

Yesterday, all my firebase functions started throwing the following warning:

Firestore中存储的Date对象的行为将改变 而且您的应用可能会崩溃.隐藏此警告并确保您的应用确实 不间断,您需要先在应用中添加以下代码 调用其他任何Cloud Firestore方法:

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);

进行此更改后,将读取存储在Cloud Firestore中的时间戳记 作为Firebase Timestamp对象而不是作为系统Date对象返回. 因此,您还需要更新代码以期望使用Date来代替 期待一个时间戳.例如:

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();

启用新功能后,请审核Date的所有现有用法 行为.在将来的版本中,行为将更改为新的 行为,因此,如果您不执行这些步骤,则您的应用可能会崩溃.

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.我正在使用打字稿.

Now I want to init my firestore correctly according to this warning. i'm using typescript.

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';    
admin.initializeApp(functions.config().firebase);
let fireStoreDB = fireStoreDB || admin.firestore()

从admin返回的firestore()没有警告中所述的.settings()方法,也没有在构造函数中获取对象. (它获取一个App对象). 所以我有两个问题:

the firestore() that return from admin doesn't have a .settings() method as described in the warning nor it gets an object in the constructor. (It gets an App Object). so I have two questions:

  1. 如何初始化我的Firestore以获取设置对象?

  1. How can init my firestore to get the settings object?

当我在文档中插入日期或查询文档时,还需要传递Firestore.Timestamp对象吗?还是我可以查询/插入普通的JS Date对象,它将自动转换?

when I insert a date to a document or when I query a document do I also need to pass the Firestore.Timestamp object? or can i query/insert the normal JS Date object and it will get converted automatically?

谢谢.

我设法使用

if (!fireStoreDB){
    fireStoreDB = admin.firestore();
    fireStoreDB.settings(settings);
}

但是,它仍然在Firestore触发器上发生. 任何人都知道如何在上为管理员提供默认设置:

But it still is happening on firestore triggers. anyone knows how to give default settings to admin on :

admin.initializeApp(functions.config().firebase);

那么它不会在Firestore触发器上发生吗?

so it will not happen on firestore triggers?

推荐答案

由于存在错误,您仍在接收JS日期而不是Firestore时间戳...现在已在Firebase Functions v2.0.2中修复. 请参阅: https://github.com/firebase/firebase-functions/releases /tag/v2.0.2 .

You are still receiving JS Date instead of Firestore Timestamp due to a bug... now fixed in Firebase Functions v2.0.2. See: https://github.com/firebase/firebase-functions/releases/tag/v2.0.2.

对于初始化,我使用了警告消息中指定的admin.firestore().settings({timestampsInSnapshots: true}),因此警告已消失.

For initialisation I've used admin.firestore().settings({timestampsInSnapshots: true}) as specified in warning message, so the warning has disappeared.

将日期添加到文档中或用作查询参数时,可以使用常规的JS Date对象.

When you add a date to a Document, or use as a query parameter, you can use the normal JS Date object.

这篇关于Firestore移至Firebase函数中的时间戳记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆