5

I keep getting a "XMLHttpRequest is not defined" when attempting to get the downloadURL of images I just uploaded to storage. Any thoughts on what is going on here? I can retrieve the metadata, but the image url is not listed in the scheme :/

Node:

import firebase, { storage } from './firebase';
const serviceAccount = require('./serviceAccountKey.json');
const admin = require('firebase-admin');
const app = express()
const dbUrl = "https://authentication.firebaseio.com"

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: dbUrl,
  storageBucket: 'authentication.appspot.com'
});

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

......
bucket.upload(imageUrl).then(result => {
        const file = result[0];
        return file.getMetadata();
      }).then(results => {
          const ref = storage.ref('users/' + userRecord.uid + '/image').downloadURL();
          console.log(ref)
          //const metadata = results[0];
          //console.log('metadata=', metadata.mediaLink);
          //firebase.database().ref('users/' + userRecord.uid + '/image').set(metadata.mediaLink);
      }).catch(error => {
          console.error(error);
      });
    })
    .catch(function(error) {
      console.log("Error creating new user:", error);
    });

Storage: enter image description here

user992731
  • 3,400
  • 9
  • 53
  • 83

3 Answers3

7

I figured out that using a global XMLHttpRequest was sufficient so, I did

npm install xhr2

and added

global.XMLHttpRequest = require("xhr2");

to the top of my file. It worked for me.

Rohan Kakar
  • 81
  • 1
  • 3
4

The Firebase client SDKs for JavaScript are mostly not supported for use on NodeJS. This explains your error - XMLHttpRequest is natively available on browsers but not in node. If you want to run server-side code that accesses Firebase and Google Cloud resources, you should be using the server SDKs.

Firebase provides server SDKs via the Firebase Admin SDK, which fully works on node. For Cloud Storage access, the Admin SDK repackages the existing Cloud Storage SDK provided by Google Cloud.

Note that there is no concept of a "download URL" provided by the Google Cloud SDK for Cloud Storage. It has something called a "signed URL" to use similarly.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • I am using the admin sdk – user992731 Apr 14 '19 at 01:17
  • Actually, you are not. This is not the admin SDK: `import firebase, { storage } from './firebase';`. That's the client SDK. You do import the admin SDK, but you aren't using it. – Doug Stevenson Apr 14 '19 at 01:24
  • Actually.... OP is importing both: `import firebase, { storage } from './firebase';` and `const admin = require('firebase-admin');`. You are right though that the error is caused by trying to use the Firebase SDK for web clients in a Node.js script, which isn't supported. – Frank van Puffelen Apr 14 '19 at 03:58
  • @FrankvanPuffelen The question was edited to show firebase-admin after my answer, but it still stands that they aren't actually using it to access data. – Doug Stevenson Apr 14 '19 at 04:13
  • is there any way to use a regular user account with the admin sdk? I don't need an admin account for my use case. – Patrick Michaelsen Jun 24 '21 at 04:13
1

npm install xhr2 worked for me. Please note, in general, this is not good practice. If you are working with Firebase Cloud Storage, at the nodejs level (server), you should be using the admin sdk (for servers), not the firebase client sdk. This means that you will be using google cloud functions.

terry feng
  • 11
  • 1