【问题标题】:Firebase 9 uploading photoFirebase 9 上传照片
【发布时间】:2021-12-02 11:16:42
【问题描述】:

我尝试使用代码为 firebase 9 应用照片上传:

const uploadTask = storage.ref(`images/${image.name}`).put(image);

和:

            storage
                .ref("images")
                .child(image.name)
                .getDownloadUrl()
                .then(url => { 
                    // post image inside the database
                    db.collection("posts").add({ 
                        timestamp: Firebase.ServerValue.TIMESTAMP,
                        caption: caption, 
                        imageUrl: url, 
                        username: username
                    });

                    // set progress back to 0 once it's done! and reset everything else 
                    setProgress(0);
                    setCaption("");
                    setImage(null);
                })

但是,在运行我的代码时,出现错误: 类型错误:firebase__WEBPACK_IMPORTED_MODULE_2_.storage.ref 不是函数

我该如何解决这个问题?

【问题讨论】:

    标签: javascript firebase-storage


    【解决方案1】:

    您显示的代码对于 firebase 8 看起来是正确的,但您说您使用的是版本 9。版本 9 有一些重大变化。因此,您要么需要回滚到版本 8,要么更新您的代码以使用新模式。例如,您的第一个代码 sn-p 现在是这样完成的:

    import { getStorage, ref, uploadBytes } from "firebase/storage"
    
    const storage = getStorage();
    const storageRef = ref(storage, `images/${image.name}`);
    
    const uploadTask = uploadBytes(storageRef);
    

    您可以在此处查看更多信息和示例:https://firebase.google.com/docs/storage/web/upload-files

    关于如何升级的说明可以在这里找到:https://firebase.google.com/docs/web/modular-upgrade

    【讨论】:

      猜你喜欢
      • 2017-05-09
      • 2019-04-18
      • 2017-11-20
      • 1970-01-01
      • 2021-10-09
      • 1970-01-01
      • 2021-10-18
      • 2017-03-30
      • 2020-06-24
      相关资源
      最近更新 更多