'TypeError: storage.ref is not a function 的错误

时间:2021-05-20 09:54:32

标签: javascript reactjs firebase firebase-storage

这是 buttonUploadClicked 函数,我已将 firebasestorage 导入为 const storage = firebase.storage; 但错误仍然存​​在

这个函数在点击按钮时运行,只有在点击按钮时才会显示错误

代码如下

const handleUpload = () => {
  var imageName = Math.random(0, 10000) * Math.random(0, 100);
  if (image) {
    const uploadTask = storage.ref(`images/${imageName}.jpg`).put(image);

    uploadTask.on(
      'state_changed',
      (snapshot) => {
        // progress function beta

        const progressP = Math.round((snapshot.bytesTransferred / snapshot.totalBytes) * 100);
        setProgress(progressP);
      },
      (error) => {
        console.log(error);
      },
      () => {
        storage
          .ref('images')
          .child(`${imageName}.jpg`)
          .getDownloadURL()
          .then((imageUrl) => {
            db.collection('posts').add({
              timestamp: firebase.firestore.FieldValue.serverTimestamp(),
              caption: caption,
              photoUrl: imageUrl,
              username: user.email.replace('@gmail.com', '').replace('.', ''),
              picUrl: user.photoURL,
            });
          });
      },
    );
  }
};

0 个答案:

没有答案