Firebase存储:storage.ref不是函数

问题描述

我想将Firebase的存储服务与nodeJS api(托管在“ firebase函数”上)一起使用,以允许用户上传其头像。

所以我从https://firebase.google.com/docs/storage/web/start阅读了文档

我这样做:

admin.js

const admin = require('firebase-admin');
const config = require('./config.js');

admin.initializeApp(config);

const db = admin.firestore();
const storage = admin.storage();

module.exports = { admin,db,storage };

user.js

const { admin,storage } = require('../util/admin');

exports.postAvatar = async (request,response) => {

  const storageRef = storage.ref();

}

但是我遇到以下错误storage.ref is not a function

文档中是否缺少某些内容

存储常量的console.log是:

Storage {
  INTERNAL: StorageInternals {},storageClient: Storage {...},appInternal: FirebaseApp {...}
}

解决方法

尝试下面的代码。

const storage = storage.bucket() // you can also put your bucket-id from config
const storageRef = storage.ref()

也请检查此答案。 TypeError: firebase.storage is not a function

,

admin.storage()返回一个Storage对象。如果要使用它引用默认存储桶中的文件,则应使用不带参数的bucket()方法,它会为您提供来自Google Cloud nodejs SDK的Bucket对象。

该SDK中的任何地方都没有称为ref()的方法。它与JavaScript Web客户端SDK不太相似。您将必须学习其他但相似的API,才能使用Cloud Storage node SDK处理内容。 Admin SDK实际上只是包装了此API。

const file = storage.bucket().file('/path/to/file');

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...