【问题标题】:im getting error when im trying to import file on firebase storage当我尝试在 firebase 存储上导入文件时出现错误
【发布时间】:2022-11-21 18:52:13
【问题描述】:

我一直在尝试从 firebase 存储中导入文件,但我收到错误我已经阅读了文档并阅读了一些关于它的博客,但仍然是同样的错误。我只是在寻找解决这个问题的最佳方法

import { storage } from '../Firebase/firebase';
import { getStorage, ref, getDownloadURL, uploadBytesResumable } from "firebase/storage";

async function storeImage(image){
            return new Promise((resolve, reject)=>{
                const storage = getStorage()
                const filename = `${auth.currentUser.uid}-${image.name}-${uuidv4()}`
                const storageRef = ref(storage, filename)
                const uploadTask = uploadBytesResumable(storageRef, file);
                uploadTask.on('state_changed', 
                    (snapshot) => {
            // Observe state change events such as progress, pause, and resume
            // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
            const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
            console.log('Upload is ' + progress + '% done');
            switch (snapshot.state) {
            case 'paused':
                console.log('Upload is paused');
                break;
            case 'running':
                console.log('Upload is running');
                break;
            }
        }, 
        (error) => {
            // Handle unsuccessful uploads
            reject(error)
        }, 
        () => {
            // Handle successful uploads on complete
            // For instance, get the download URL: https://firebasestorage.googleapis.com/...
            getDownloadURL(uploadTask.snapshot.ref).then((downloadURL) => {
              resolve(downloadURL);
            });
        }
        );
                    })

            }

        
        const ImgUrl = await Promise.all(
            [...images].map((image)=>storeImage(image)).catch((error)=>{
                setLoading(false);
                toast.error("images cannot be uploaded")
                return;
            })
        )
        console.log(ImgUrl)
        
        

         
    }
this is the my config file have been ive imported but isn't working 

import { initializeApp } from "firebase/app";
import { getStorage } from "firebase/storage";

const firebaseConfig = {
   piKey: "AIzaSyBBP9H3mLn7Nz8NeCo4Rve87YszflMshZ8",
  authDomain: "realtor-e7363.firebaseapp.com",
  projectId: "realtor-e7363",
  storageBucket: "realtor-e7363.appspot.com",
  messagingSenderId: "960865969593",
  appId: "1:960865969593:web:9fde225122d9555ec00268",
};

// Use this to initialize the firebase App
// Use these for db & auth
const db = firebaseApp.firestore();
const auth = firebase.auth();
const app = initializeApp(firebaseConfig);
export const storage = getStorage(app);
export { auth, db };

我试过导入文件并阅读了一些文档,但没有成功

【问题讨论】:

    标签: reactjs firebase


    【解决方案1】:

    您的 Firebase 配置文件需要进行如下调整(另请参阅 doc):

    import { initializeApp } from "firebase/app";
    import { getStorage } from "firebase/storage";
    import { getFirestore } from "firebase/firestore";
    import { getAuth } from "firebase/auth";
    
    const firebaseConfig = {
      //...
    };
    
    
    const app = initializeApp(firebaseConfig);
    const auth = getAuth(app);
    const db = getFirestore(app);
    const storage = getStorage(app);
    
    export { auth, db, storage };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 2011-06-02
      • 2015-02-07
      • 2021-06-02
      • 2020-07-09
      • 2016-09-18
      • 2020-04-23
      相关资源
      最近更新 更多