I'm trying to write some test data to a local version of Firebase. I have these variables defined in my .env.local (and .env.development files) ...
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
I have the below set up (via npm run dev:upload) to upload dummy data into my local 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
adly the above results in
...
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
or all data in my data.json file (examples of it are below). What else do I need to configure to get this data into my local 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"
          },
You haven't passed your config object to admin.initializeApp
Since you use dotenv to load env files, you can create a config object with env details like
const config = {
    type: "service_account",
    project_id: "xxx",
    credential: 'your credentials'
};
require("dotenv").config();
  admin.initializeApp({config});
  db = admin.firestore();
If you are not passing any config to initializeApp then you need to define it FIREBASE_CONFIG env object.
According to the documentation:
The SDK can also be initialized with no parameters. In this case, the SDK uses Google Application Default Credentials and reads options from the
FIREBASE_CONFIGenvironment variable. If the content of theFIREBASE_CONFIGvariable begins with a { it will be parsed as a JSON object. Otherwise the SDK assumes that the string is the name of a JSON file containing the options.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With