Hey guys I have a problem relating to firebase, so the problem is whenever i upload the same photo twice i got an error that the previous photos go unfetched(that's a blank img icon) due to having same names,so i installed a library 'uuidv4' which generates unique ids, so how can i put and save the unique id instead of given image name
let uniqueId = uuidv4()
const uploadTask = storage.ref(`images/${image.name}`).put(image);
then i did this
let uniqueId = uuidv4()
const uploadTask = storage.ref(`images/${uniqueId}`).put(image);
but got an error
"Firebase Storage: Invalid argument in put at index 0: Expected Blob or File.
The upload function(i have jsx below that I didn't include at the bottom so don't worry about the closing brackets)
function ImageUpload({username}) {
const [image, setImage] = useState(null);
const [progress, setProgress] = useState(0);
const [caption, setCaption] = useState('');
const handleChange = (e) => {
if (e.target.files[0]) {
setImage(e.target.files[0]);
}
};
const handleUpload = () => {
let uniqueId = uuidv4()
const uploadTask = storage.ref(`images/${image.name}`).put(image);
uploadTask.on(
"state_changed",
(snapshot) => {
const progress = Math.round (
(snapshot.bytesTransferred / snapshot.totalBytes) * 100
);
setProgress(progress);
},
(error) => {
console.log(error);
alert(error.message);
},
() => {
storage
.ref("images")
.child(image.name)
.getDownloadURL()
.then(url => {
db.collection("posts").add({
timestamp: firebase.firestore.FieldValue.serverTimestamp(),
caption: caption,
imageUrl: url,
username: username
})
})
setProgress(0)
setCaption('')
setImage(null)
}
)
}
return(jsx)
}