json - 将 JSON 导入 Firebase Firestore

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

在有人将此标记为重复之前,我知道: 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,这些是 //要求使用 === 和 !==//不允许在没有类型检查运算符的情况下进行空比较//不允许在循环内等待。现在,经过这些修改,当我在终端中的函数文件夹中输入 firebase deploy --only functions 时,我被告知该函数已部署并提供了指向我的 Firebase 控制台的链接。但是,我的函数没有出现在firebase的函数选项卡中,我的数据也没有上传到云firestore。有谁知道我做错了什么?难道我没有在函数上调用 exports 吗?或者这仅用于预构建的 Firebase 函数?

最佳答案

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

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

关于json - 将 JSON 导入 Firebase Firestore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49083301/

相关文章:

java - Spring是否将字节数组解释为base64编码数据

json - 如何使用 jQuery 创建 JSON 对象

node.js - 如果出现不可恢复的错误,如何在 Heroku 中强制重启 NodeJS 进程?

node.js - Node JS body-parser 获取 rawBody 和 body

.net - 如何在 ASP.NET MVC 中将 View 模型转换为 JSON 对象?

json - 为什么我在快速解析时没有在按钮操作中获取所有 cellForRowAtindexPath 文本字段值

Json对象与唯一id合并

ios - 尝试显示身份验证选择器时 FirebaseUI 崩溃

android - 从android中的firebase数据库获取用户排名

javascript - Rxjs 订阅队列