在本地Firebase中,如何为当前环境设置projectId?

问题描述 投票:1回答:1

我正在尝试将一些测试数据写入Firebase的本地版本。我在.env.local(和.env.development文件)中定义了这些变量...

REACT_APP_MB_ACCESS_TOKEN="accesstoken"
ALGOLIA_APP_ID="appid"
ALGOLIA_API_KEY="apikey"
NODE_ENV="development"
FIREBASE_EMULATOR_HOST_VAR=localhost:9000
FIRESTORE_EMULATOR_HOST=localhost:8080
GCLOUD_PROJECT=mutualaid-123f6

我进行了以下设置(通过npm run dev:upload)将虚拟数据上传到我的本地Firebase中...

const admin = require("firebase-admin");

const data = require("./data.json");
let serviceAccount = process.env.FIREBASE_SECRET;
...

  require("dotenv").config();
  admin.initializeApp();
  db = admin.firestore();
...

async function upload(data, path) {
  console.log("starting ...");
  return await db
    .doc(path.join("/"))
    .set(data)
    .then(() => console.log(`Document ${path.join("/")} uploaded.`))
    .catch((e) => {
      console.error(`Could not write document ${path.join("/")}.`);
      console.log(e);
    });

S

以上结果全部通过

...
Could not write document organizations/1/missions/aaf3c03c33ecf79e3d7ffddf401c01f0.
Error: Unable to detect a Project Id in the current environment. 
To learn more about authentication and Google APIs, visit: 
https://cloud.google.com/docs/authentication/getting-started
    at /Users/davea/Documents/workspace/resilience-app/node_modules/google-auth-library/build/src/auth/googleauth.js:89:31
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

f

或我的data.json文件中的所有数据(下面是示例)。我还需要配置什么才能将这些数据导入本地Firebase?

{
  "organizations": {
    "1": {
      "missions": {
        "aaf3c03c33ecf79e3d7ffddf401c01f0": {
          "status": "started",
          "missionDetails": {
            "dummy": "This is only here because upload modules did not understand that we want this to be a field",
            "needs": [
              {
                "name": "Fruits & Veggies Medley",
                "quantity": 1
              }
            ]
          },
          "uid": "aaf3c03c33ecf79e3d7ffddf401c01f0",
          "deliveryConfirmationImage": "",
          "feedbackNotes": "",
          "groupUid": "Daly City 2020/03/05",
          "organizationUid": "1",
          "volunteerDisplayName": "Michael Williams",
          "deliveryWindow": {
            "timeWindowType": "as soon as possible",
            "startTime": "06/03/2020, 10:40:57"
          },
reactjs firebase npm google-cloud-firestore google-authentication
1个回答
0
投票

似乎您可能缺少process.env.FIREBASE_SECRET(除非您有意将其包括在内,因为它是私钥)。我还应该指出,.env*文件不适合存储私钥,因为它们捆绑在您的应用程序代码中并且可以公开访问。

您可以通过两种方法初始化admin SDK。它们在Add the Firebase Admin SDK to your server参考指南中概述。似乎您正在尝试初始化不带参数的SDK,已覆盖here。基本上,您需要设置一个名为export的环境变量(通过终端中的.env,而不是FIREBASE_CONFIG文件),它是包含配置信息的JSON文件的路径,还是包含配置信息的JSON对象。 。

祝你好运!

© www.soinside.com 2019 - 2024. All rights reserved.