获取下载URL使用Firebase不断抛出错误``未定义XMLHttpRequest'' [英] Get downloadURL keeps throwing error 'XMLHttpRequest is not defined' using Firebase

查看:53
本文介绍了获取下载URL使用Firebase不断抛出错误``未定义XMLHttpRequest''的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试获取刚上传到存储中的图像的downloadURL时,我一直收到未定义XMLHttpRequest"的消息.对这里发生的事情有什么想法吗?我可以检索元数据,但是该方案中未列出图片网址:/

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 :/

节点:

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);
    });

存储:

推荐答案

在NodeJS上,大多数不支持JavaScript的Firebase客户端SDK.这解释了您的错误-XMLHttpRequest在浏览器中本机可用,但在节点中不可用.如果您要运行访问Firebase和Google Cloud资源的服务器端代码,则应使用服务器SDK.

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通过 Firebase管理SDK 提供了服务器SDK,该工具在节点上完全可用.对于Cloud Storage访问,Admin SDK重新打包了Google Cloud提供的现有Cloud Storage SDK.

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.

请注意,Google Cloud SDK for Cloud Storage没有提供下载URL"的概念.它具有类似的使用方式,称为签名URL".

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.

这篇关于获取下载URL使用Firebase不断抛出错误``未定义XMLHttpRequest''的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆