![](/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.