问题描述
我想将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');