【问题标题】:Importing JSON into Firebase Firestore将 JSON 导入 Firebase Firestore
【发布时间】:2018-08-11 12:07:23
【问题描述】:

在任何人将此标记为重复之前,我都知道: 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 的功能选项卡中,我的数据也没有上传到云 Firestore。有谁知道我做错了什么?难道是我没有在函数上调用exports?还是仅用于预构建的 Firebase 功能?

【问题讨论】:

  • 这个 fakedb.json 是什么?它是导出的 json 的常量名称吗?它存储在哪里?在本地机器上?
  • 可以分享一下你的数据迁移项目吗?
  • 嗨@Sony,这是您要上传到 Firestore 的 JSON 文件。在运行脚本之前,将您的 JSON 复制并粘贴到在线验证器(例如 jsonlint.com)以确保其有效。文件本身应与脚本位于同一文件夹中。 ./fakedb.json 是文件位置,其中“.”本质上表明它在当前文件夹中。您需要从 Firebase 下载您的服务密钥,您可以在此处找到有关如何执行此操作的说明:hackernoon.com/filling-cloud-firestore-with-data-3f67d26bd66e,在“身份验证”下。
  • 我搞定了,谢谢你的时间 :-)

标签: json node.js firebase google-cloud-firestore


【解决方案1】:

该脚本不是 Cloud Function,它看起来需要简单地作为 Node.js 独立脚本运行,data 从 RTDB 获得您想要在 Cloud Firestore 中的任何数据。

如果您想将其用作云函数,则需要设置一些导出并将其中的数据用作data 的值。注意 - 这将只允许您传输更改/读取的数据。您可能希望至少首先对整个数据库运行它(因此它是一个独立的脚本)。

【讨论】:

  • 您先生,绝对是个天才!非常感谢您花时间提供帮助!对于其他有兴趣让这个脚本工作的人,我通过在终端中输入node index.js 来运行它。非常感谢丹!
猜你喜欢
  • 2018-03-20
  • 2018-05-23
  • 2020-06-02
  • 1970-01-01
  • 2018-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多