node.js - nodejs Firestore突然崩溃: Date objects

标签 node.js firebase google-cloud-firestore

我运行一个简单的 Node 脚本来更新 Firestore 数据库中的数据。我几个小时前使用过,效果很好。吃晚饭回来了,现在运行它时出现此错误:

node ./json-to-firestore.js 

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

错误中提供的示例不适用于我的情况。我已经在这个问题上寻求帮助,但所有帖子似乎都是特定于角度的。这就是我想做的一切:

var admin = require("firebase-admin");

var serviceAccount = require("./service-key.json");

const data = require("./occ-firestore.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "xxxxxxxxxxx"
});

data && Object.keys(data).forEach(key => {
    const nestedContent = data[key];

    if (typeof nestedContent === "object") {
        Object.keys(nestedContent).forEach(docTitle => {
            admin.firestore()
                .collection(key)
                .doc(docTitle)
                .set(nestedContent[docTitle])
                .then((res) => {
                    console.log("Document successfully written!");
                })
                .catch((error) => {
                    console.error("Error writing document: ", error);
                });
        });
    }
});

我通过运行以下命令来执行此脚本:

node ./json-to-firestore.js

我正在运行 NodeJS 8.11.3。

我检查了Google's docs并且没有提及这种新行为。

有人可以给我一个建议吗?提前致谢!

最佳答案

这可以这样修复:

const admin = require('firebase-admin');
const functions = require('firebase-functions');

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

const db = admin.firestore();
db.settings({ timestampsInSnapshots: true });

关于node.js - nodejs Firestore突然崩溃: Date objects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51390958/

相关文章:

node.js - 使用 oracle db 在 Node 中进行多个 SQL 查询

node.js - 使用 Knex.js 和 PostgreSQL 设置 Docker

node.js - Towerjs 中的布局/ View

flutter - 停止在 flutter 中收听 Cloud Firestore 中的快照更新

javascript - 带有express-generator的Nodejs应用程序,并尝试添加primus websocket

python-3.x - 在本地测试 Python Google Cloud Functions

ruby-on-rails - 使用 FCM gem RAILS 发送通知时出现身份验证错误

swift - 数据库引用错误

android - 无法让 FirestoreRecyclerAdapter 显示项目

安卓火力地堡 : Update/Add record in Nested Array data