这是 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,
});
});
},
);
}
};