【问题标题】:I am trying to run a script but the steps I have taken are not working. Where have I gone wrong?我正在尝试运行脚本,但我采取的步骤不起作用。我哪里做错了?
【发布时间】:2026-01-19 21:55:01
【问题描述】:

我正在尝试运行 Maciej Caputa 编写的脚本,此处显示为答案:How to import CSV or JSON to firebase cloud firestore

我的目标是使用 JSON 文件将数据上传到 Cloud Firestore 我是运行脚本的新手,所以如果有人能指出我如何运行这个脚本的方向。我已经概述了我在下面尝试过的步骤。我哪里做错了?

到目前为止我所尝试的:

  1. 我已将脚本代码保存在文本文件中。

  2. 我用我的数据库的 url 填写了 DatabaseURL 代码行。

  3. 我已经使用终端安装了node和nom。

  4. 在终端中我做了:sudo(我的​​脚本文件的路径)

  5. 然后做了:节点(子文件的路径>)

但这不起作用。

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

【问题讨论】:

    标签: javascript node.js firebase google-cloud-firestore firebase-admin


    【解决方案1】:

    对于第 5 步,您需要提供要使用 node 执行的文件的路径。它应该类似于node yourFileName.js

    【讨论】:

    • 抱歉,我就是这么做的。在我的步骤中,我使用了 而不是 (),这导致我的话没有出现。我刚刚修好了。无论如何,当我引用路径时它不起作用。
    • 这似乎是一个与 PATH 相关的问题,不包括节点可执行文件的位置。我不知道您正在运行哪个操作系统,所以请研究如何为您的操作系统添加节点到 PATH。
    最近更新 更多