reactjs - 在本地 Firebase 中,如何为当前环境设置 projectId?
问题描述
我正在尝试将一些测试数据写入本地版本的 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"
},
解决方案
您尚未将配置对象传递给admin.initializeApp
由于您使用 dotenv 加载 env 文件,因此您可以创建一个带有 env 详细信息的配置对象,例如
const config = {
type: "service_account",
project_id: "xxx",
credential: 'your credentials'
};
require("dotenv").config();
admin.initializeApp({config});
db = admin.firestore();
如果您没有将任何配置传递给 initializeApp,那么您需要将其定义为FIREBASE_CONFIG 环境对象。
根据文档:
SDK 也可以不带参数初始化。
FIREBASE_CONFIG
在这种情况下,SDK 使用 Google 应用程序默认凭据并从环境变量中读取选项。如果FIREBASE_CONFIG
变量的内容以 { 开头,它将被解析为 JSON 对象。否则,SDK 假定该字符串是包含选项的 JSON 文件的名称。
推荐阅读
- c++ - 在 C++ 中使用另一个向量对一个向量进行 Lambda 排序
- .net - 使用超链接将 RDLC 渲染为 PDF
- matlab - 将归一化滤波器转换为实频滤波器
- excel - 将文件嵌入特定列并向下一行插入另一个文件
- asp.net - 如何更改 Url.Action() 使用的根 URL?
- java - 我们是否需要在 Spring Boot 中为每个表编写实体类?
- amazon-web-services - 在 aws 上创建 https 重定向的步骤
- java - 如何在方法之间调用字符串?
- django - Django Graphene - 将 info.context 传递给 Query 或 Mutation 的装饰器
- python - pandas 序列的唯一 ID