gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:43:21 26 4
gpt4 key购买 nike

我运行一个简单的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com