TypeError: firebase.storage is not a function

前端 未结 14 1202
予麋鹿
予麋鹿 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:23

    Deprecated: please see the accepted answer.

    Some details to note:

    1. Firebase Storage is no longer used with Node.js, so all documentation there is useless for Node.js. Instead, use google-cloud. The references and guides for Firebase and Google Cloud do not reflect this as of today.
    2. Unlike Firebase, google-cloud costs money, even for small projects.
    3. In my case, I'm using the firebase-admin SDK so I don't have to mess with user authentication at the moment.

    Purpose

    To create a single Node.js project which uses Firebase and Google Cloud. Why? Firebase has a useful database, among other features, and Google Cloud allows cloud file storage and retrieval.

    Directions

    Step 1: Project Creation

    Create Firebase and Google Cloud (Storage) projects.

    Step 2: Install Packages

    Using npm, install firebase-admin and google-cloud in Node.js project.

    Note 1: I used the admin SDK, so after creating the Firebase project, you'll need to go to:

    • Settings(the gear) > Project Settings > Service Accounts > Firebase Admin SDK
    • Then you: Select Node.js > [Copy/paste the generated code into your project] > [click "Generate New Private Key"] > [download the generated json to preferred location] > [replace "path/to...AccountKey.json" with the path to the key you just generated]

    Note 2: the generated key can be reused in firebase or google-cloud credentials.

    Step 3: Firebase Setup

    Once your project is created, import the firebase-admin sdk:

    The code should look like this, but filled with your info:

    var admin = require("firebase-admin");
    admin.initializeApp({
      credential: admin.credential.cert("/path/to/generated/json/here.json"),
      databaseURL: "database-url-from-firebase"
    });
    

    To find the databaseURL, go to 'Storage' in Firebase, and note the URL starting with gs: and copy/paste it the the value field of databaseURL.

    Next, get a reference to the database you can use:

    var db = admin.database();
    var ref = db.ref("/");
    console.log('DB ref: ' + ref); //just to debug, if undefined, there's a problem.
    

    To learn more about reading/writing to the database, follow Firebase's own documentation.

    Step 4: Google-Cloud Billing Setup

    After creating a project on Google Cloud, add billing information; buckets cannot be used without billing info.

    Step 5: Google-Cloud Storage Setup

    1. Scrolling through the menu (the horizontal 3-bars), click "Storage", then "Enable Billing". Yes, you added billing info, now you need to enable it for that project's buckets.
    2. You should see that a bucket should already exists from your Firebase project.
    3. Click on menu again(3-bar icon), then > IAM & Admin > Settings
    4. At settings, you'll see "Project ID" which should look like "projectName-00000" or "projectName-Some#", copy that project ID

    Step 6: Google Cloud in Node.js

    In your index.js:

    var gcloud = require('google-cloud');
    var gcs = gcloud.storage({
      projectId: 'paste-that-project-id-here',
      keyFilename: 'paste-that-path-to-the-previously-downloaded-json-from-firebase-here'
    });
    

    Now you can send a file to your storage by:

    var bucket = gcs.bucket('bucket_name');
    var remoteFile = bucket.file('somefile-inThisCaseASong.mp3');
    var localFilename = '/Users/you/Music/somefile-inThisCaseASong.mp3';
    bucket.upload(localFilename, function(err, file) {
      if (!err) {
        console.log('somefile-inThisCaseASong.mp3 is now in your bucket.');
      } else {
        console.log('Error uploading file: ' + err);
      }
    });
    

    Step 7: Verify

    If the file is visible in Firebase Storage and Google Cloud Storage, you're done!

提交回复
热议问题