Firebase Cloud功能-更改Firestore设置 [英] Firebase Cloud Functions - Change Firestore settings
问题描述
我正在使用Firebase Cloud Function,自最近以来,日志向我显示此消息:
I'm using Firebase Cloud Function and since recently, the logs show me this message:
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);
问题是,当我将这段代码添加到函数文件中时,每次尝试部署时都会出现此错误:
The problem is that when I add that piece of code into my functions file, I get this error every time I try to deploy:
ReferenceError: Firestore is not defined
有人可以帮我找出可能出问题的地方吗?
Can somebody help me find what could be wrong?
(我是否需要在package.json文件中添加Firestore依赖项?即使在我已经使用Firestore功能的情况下不需要这样做?)
(Do I need to add a Firestore dependence in the package.json file? Even if I don't need to do it whereas I already use the Firestore features?)
谢谢
推荐答案
这应该可以完成工作:
const admin = require('firebase-admin');
admin.initializeApp();
const firestore = admin.firestore();
// Add this magical line of code:
firestore.settings({ timestampsInSnapshots: true });
然后在您的函数中直接使用firestore对象:
Then in your function use the firestore object directly:
firestore.doc(`/mycollection/${id}`).set({ it: 'works' })
截至2019年3月的更新:
如果使用的是firebase-admin
7.0.0或更高版本,则不再需要此修复程序.
If using firebase-admin
version 7.0.0 or above, you no longer need this fix.
在版本7.0.0-2019年1月31日中firebase-admin
引入了一些重大更改:>
In Version 7.0.0 - January 31, 2019 of firebase-admin
there were some breaking changes introduced:
BREAKING:
timestampsInSnapshots
默认设置已更改为true.
BREAKING: The
timestampsInSnapshots
default has changed to true.
timestampsInSnapshots
设置现在默认启用,因此时间戳
从DocumentSnapshot
读取的字段将作为Timestamp
对象返回
而不是Date
.任何期望接收Date
对象的代码都必须是
已更新.
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.
此外,如官方文档中所述,timestampsInSnapshots
将在以后的版本中删除,因此请确保将其完全删除.
What's more, as stated in the official docs, timestampsInSnapshots
is going to be removed in a future release so make sure to remove it altogether.
这篇关于Firebase Cloud功能-更改Firestore设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!