在任何人认为这是重复之前,我都知道: How to import CSV or JSON to firebase cloud firestore
如上所述,我正在尝试使用Google Cloud Function将JSON发送到Firestore,允许我将实时数据库转换为Firestore。我一直在使用Maciej Kaputa在上面的链接中提供的脚本作为答案。他提供的脚本如下,与上面链接中提供的脚本完全相同:
const admin = require('../functions/node_modules/firebase-admin');
const serviceAccount = require("./service-key.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://<your-database-name>.firebaseio.com"
});
const data = require("./fakedb.json");
/**
* Data is a collection if
* - it has a odd depth
* - contains only objects or contains no objects.
*/
function isCollection(data, path, depth) {
if (
typeof data != 'object' ||
data == null ||
data.length === 0 ||
isEmpty(data)
) {
return false;
}
for (const key in data) {
if (typeof data[key] != 'object' || data[key] == null) {
// If there is at least one non-object item then it data then it cannot be collection.
return false;
}
}
return true;
}
// Checks if object is empty.
function isEmpty(obj) {
for(const key in obj) {
if(obj.hasOwnProperty(key)) {
return false;
}
}
return true;
}
async function upload(data, path) {
return await admin.firestore()
.doc(path.join('/'))
.set(data)
.then(() => console.log(`Document ${path.join('/')} uploaded.`))
.catch(() => console.error(`Could not write document ${path.join('/')}.`));
}
/**
*
*/
async function resolve(data, path = []) {
if (path.length > 0 && path.length % 2 == 0) {
// Document's length of path is always even, however, one of keys can actually be a collection.
// Copy an object.
const documentData = Object.assign({}, data);
for (const key in data) {
// Resolve each collection and remove it from document data.
if (isCollection(data[key], [...path, key])) {
// Remove a collection from the document data.
delete documentData[key];
// Resolve a colleciton.
resolve(data[key], [...path, key]);
}
}
// If document is empty then it means it only consisted of collections.
if (!isEmpty(documentData)) {
// Upload a document free of collections.
await upload(documentData, path);
}
} else {
// Collection's length of is always odd.
for (const key in data) {
// Resolve each collection.
await resolve(data[key], [...path, key]);
}
}
}
resolve(data);
但是,这对我来说似乎不起作用。我以通常的方式设置了我的firebase cli,并通过成功运行标准的“Hello world”函数来检查函数是否可以发送到我的项目。我的JSON不是问题,因为我使用过在线验证器。通过一些研究,我发现eslint ecmaVersion 6不允许异步函数,可以通过将.eslintrc.json文件中的ecmaVersion更改为“ecmaVersion”:2017来解决。接下来,您必须从.eslintrc中删除一些规则.json,这些是// Require the use of === and !==
,// Disallow null comparisons without type-checking operators
和// Disallow await inside of loops
。现在,通过这些修改,当我在终端中的函数文件夹中键入firebase deploy --only functions
时,我被告知该功能已部署并提供了指向我的Firebase控制台的链接。但是,我的功能没有出现在firebase的功能选项卡中,我的数据也没有上传到云端防火墙。有谁知道我做错了什么?可能是因为我没有在函数上调用exports
吗?或者这仅用于预构建的Firebase功能?
答案 0 :(得分:4)
该脚本不是云功能,看起来只需要运行一个Node.js独立脚本,var data = [{
id: 23,
name: 'AeriaGames Login',
player_limit: -1,
player_online: -1,
state: 2,
last_update: '2018-02-14T16:22:28.000Z'
},
{
id: 1,
name: 'Auth',
player_limit: -1,
player_online: -1,
state: 2,
last_update: '2018-03-03T14:17:22.000Z'
},
{
id: 10,
name: 'English 1 (Europe)',
player_limit: 4000,
player_online: 871,
state: 2,
last_update: '2018-03-03T14:17:22.000Z'
},
{
id: 11,
name: 'English 2 (Europe)',
player_limit: 4000,
player_online: 48,
state: 2,
last_update: '2018-03-03T14:17:22.000Z'
},
{
id: 12,
name: 'English 3 (North America)',
player_limit: 4000,
player_online: 54,
state: 2,
last_update: '2018-03-03T14:17:22.000Z'
}
]
var getPlayerOnline = data.find(obj => obj.id === 12).player_online
console.log(getPlayerOnline)
在Cloud Firestore中提供您想要的RTDB数据。
如果要将其用作云功能,则需要设置一些导出并将其中的数据用作data
的值。注意 - 这只允许您传输已更改/读取的数据。您可能希望至少在第一个数据库中针对整个数据库运行此操作(因此它是一个独立的脚本)。