【问题标题】:firebase storage: storage.ref is not a functionfirebase 存储: storage.ref 不是函数
【发布时间】:2021-01-29 12:57:21
【问题描述】:

我想将 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, db, 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 {...}
}

【问题讨论】:

    标签: javascript node.js firebase-storage firebase-admin


    【解决方案1】:

    试试下面的代码。

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

    还要检查这个答案。 TypeError: firebase.storage is not a function

    【讨论】:

      【解决方案2】:

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

      该 SDK 中的任何地方都没有名为 ref() 的方法。它不像 JavaScript Web 客户端 SDK。在使用Cloud Storage node SDK 时,您必须学习不同但相似的 API 来处理内容。 Admin SDK 实际上只是封装了这个 API。

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

      【讨论】:

        猜你喜欢
        • 2022-01-14
        • 2021-08-09
        • 2021-12-26
        • 2020-04-02
        • 2018-07-18
        • 2019-10-19
        • 2022-10-07
        • 2020-12-11
        • 2021-12-18
        相关资源
        最近更新 更多