TypeError: firebase.storage is not a function

前端 未结 14 1063
予麋鹿
予麋鹿 2020-11-27 14:47

Following this example, I keep getting the error:

 TypeError: firebase.storage is not a function

From this line in my code:



        
相关标签:
14条回答
  • 2020-11-27 15:40

    When using Storage with Firebase, you're correct that you can't add buckets on the free tier. However, you DO get a bucket (just one) by default. My (eventually) successful approach was to:

    1. Add Storage to my project in Firebase (NOT Google Cloud)

    2. Add the Admin SDK and set up the necessary Service Account as per the Google Docs: https://firebase.google.com/docs/admin/setup?authuser=1

    3. Add the @google-cloud/storage package as per the instructions on using the Admin SDK with storage: https://firebase.google.com/docs/storage/admin/start?authuser=1

    4. Initialize the app:

      admin.initializeApp({
        credential: admin.credential.cert("/path/to/generated/json/here.json"),
        storageBucket: "folder-URL-from-Storage-page-excluding-gs://"
      });
      
    5. Access the bucket object with (from Admin SDK docs):

      const bucket = admin.storage().bucket();

    6. Operate on the bucket with the storage library. Example:

      bucket.upload('/path/file.ext', function(err, file, apiResponse) {
        //Do Stuff
      });
      

    NOTE: I spent a couple of hours convinced it wasn't working because I didn't have a bucket, but it turned out my mistake was including the gs:// in the path to my storage bucket when initializing.

    0 讨论(0)
  • 2020-11-27 15:41

    I encounter the same problem when I test my app locally, but everything works when the project is deployed.

    This way, I actually use the following workaround :

    if (!firebase.storage) {
        // prevent crash when working locally
        return;
    }
    let ref = firebase.storage().ref()
    // perform production stuff ...
    

    It's a little bit curious, but it works in my case.

    0 讨论(0)
提交回复
热议问题