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

查看:18
本文介绍了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();

当您启用新的日期时,请审核所有现有的使用情况行为.在未来的版本中,行为将更改为新的行为,因此如果您不遵循这些步骤,您的应用程序可能会中断.

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?

谢谢.

我设法使用以下方法解决了 http 函数的问题:

i managed to solve it for http functions using :

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.

当你在 Document 中添加日期,或者用作查询参数时,可以使用普通的 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天全站免登陆