![](/img/trans.png)
[英]I am trying to detect changes on a particular element using the MutationObserver but it is not working. What am I doing wrong?
[英]I am trying to run a script but the steps I have taken are not working. Where have I gone wrong?
我正在尝试运行Maciej Caputa编写的脚本,此处显示为答案: 如何将CSV或JSON导入 Firebase Cloud Firestore
我的目标是使用JSON文件将数据上传到Cloud Firestore,我是运行脚本的新手,所以如果有人可以向我指出如何运行此脚本的方向,我在下面概述了我尝试过的步骤。 我哪里做错了?
到目前为止我尝试过的是:
我已将脚本代码保存在一个文本文件中。
我用数据库的URL填写了DatabaseURL代码行。
我已经使用终端安装了node和nom。
在终端中,我做了:sudo(脚本文件的路径)
但这没有用。
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 in the 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);
对于第5步,您需要提供要使用node
执行的文件的路径。 它应该类似于node yourFileName.js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.