0

I am trying to run the script written by Maciej Caputa, shown as an answer here: How to import CSV or JSON to firebase cloud firestore

My goal is to use a JSON file to upload data to the Cloud Firestore I am new to running scripts, so if anyone could point me in the direction on how to run this script.I have outlined the steps I have tried below. Where did I go wrong?

What I have tried so far:

  1. I have saved the script code in a text file.

  2. I filled in the DatabaseURL line of code with the url for my database.

  3. I have installed both node and nom using terminal.

  4. In terminal I did: sudo (path to my script file)

  5. Then did: node (path to son file>)

But this did not work.

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

1 Answers1

0

For step number 5, you need to provide a path to the file you're tying to execute with node. It should be something like node yourFileName.js

alenk
  • 96
  • 6
  • Apologies, that is what I did. In my steps I had used < > instead of ( ) which caused my words not to show up. I just fixed that. Anyways, it does not work when I reference the path. – Lynn O'Brian Aug 05 '19 at 17:47
  • That seems like a problem related to PATH not including location to node executable. I don't know which OS you're running, so please research how to add node to PATH for your OS. – alenk Aug 05 '19 at 18:11