Admin SDK 无法设置 Firestore 的设置
Admin SDK cannot set settings for 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);
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.
我正在尝试在我的 Cloud Functions 代码中实施管理 SDK 中建议的更正,因为我所做的大部分工作都是通过那里进行的。
我尝试使用 admin.firestore().settings({ timestampsInSnapshots: true })
但收到以下警告:
admin.firestore(...).settings is not a function
如何解决?
我解决了:
const settings = { timestampsInSnapshots: true };
const db = admin.firestore();
db.settings(settings);
db.collection('any');
我遇到了同样的问题。我必须更新 firebase-functions 和 firebase-admin。
要升级,请转到您的 CLI,然后
ProjectDirectory > Functions > npm install firebase-functions@latest firebase-admin@latest --save
然后,在顶部,触发功能之前:
const firestore = admin.firestore();
const settings = {timestampsInSnapshots: true};
firestore.settings(settings);
为防止 "Firestore.settings() has already been called" 错误,更改
db.settings(settings);
至
try{ db.settings(settings); }catch(e){}
尝试在初始化可能有帮助的管理 SDK 后立即配置设置。
所以,我最近收到了这个警告:
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.
我正在尝试在我的 Cloud Functions 代码中实施管理 SDK 中建议的更正,因为我所做的大部分工作都是通过那里进行的。
我尝试使用 admin.firestore().settings({ timestampsInSnapshots: true })
但收到以下警告:
admin.firestore(...).settings is not a function
如何解决?
我解决了:
const settings = { timestampsInSnapshots: true };
const db = admin.firestore();
db.settings(settings);
db.collection('any');
我遇到了同样的问题。我必须更新 firebase-functions 和 firebase-admin。
要升级,请转到您的 CLI,然后
ProjectDirectory > Functions > npm install firebase-functions@latest firebase-admin@latest --save
然后,在顶部,触发功能之前:
const firestore = admin.firestore();
const settings = {timestampsInSnapshots: true};
firestore.settings(settings);
为防止 "Firestore.settings() has already been called" 错误,更改
db.settings(settings);
至
try{ db.settings(settings); }catch(e){}
尝试在初始化可能有帮助的管理 SDK 后立即配置设置。