Home > Blockchain >  No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() - Firebas
No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() - Firebas

Time:08-17

I need to upload a string to a data bucket. I am following the official firebase guide but when I write these two lines:

const storage = getStorage();
const uploadedRef = ref(storage, 'etc/savedExample/');

the function logs the error.

Apparently this is because I am invoking firebase before the app is initialized but I have called admin.InitializeApp() in my handler file therefore this cannot be true.

Could this be happening because I'm trying to use the same app reference to make querys aswell as upload files to storage?

Code that is meant to work:

//imports:
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
import { getStorage, ref } from "firebase/storage";

const storage = getStorage();
const uploadedRef = ref(storage, 'etc/savedExample/');
uploadString(uploadedRef, "testing").then(() => {
   console.log('Uploaded a raw string!');
}).catch(); //catch a potential rejected promise

CodePudding user response:

The client-side web SDK (importing from firebase or firebase/xxx) is independent of the server-side admin SDK (importing from firebase-admin or firebase-admin/xxx).

By calling admin.initializeApp(), you have prepared the admin SDK for use, but not the client SDK.

To upload a string to Cloud Storage using Node in a Cloud Function, you need to use admin.storage() (in the legacy namespaced API) or getStorage() (in the new modular API) as covered in the documentation for the Admin SDK.

import { getStorage } from 'firebase-admin/storage';

// assuming initializeApp has already been called by now
const bucket = getStorage().bucket('your-bucket-name');

// bucket is a Bucket object from the Cloud Storage SDK:
// https://googleapis.dev/nodejs/storage/latest/Bucket.html

return bucket.file('etc/savedExample')
  .save("testing")
  .then(() => console.log('uploaded a raw string'))
  .catch((err) -> console.error('failed save', err));

See File#save() for the other options for that method.

  • Related