update: i have added the entire file
I have uploaded an image to firebase storage, i then want to send the url to firebase realtime database under a node for a particular user.
I am using this code to try it, but i get an error saying
Cannot read properties of undefined (reading 'path')
here is the block of code I am using
The error points to this line ` set(ref(db,'users/'+auth.currentUser.uid+'/images/'+url))
import { getAuth } from "firebase/auth";
import React, { useState } from "react";
import { db } from "./util/firebase";
import { getDownloadURL, ref, uploadBytesResumable } from "firebase/storage";
import {storage} from './util/firebase'
import { set } from "firebase/database";
import {v4 as uuidv4} from 'uuid'
function AddPicture() {
//const [image, setImage] = useState("");
const auth = getAuth();
const user = auth.currentUser;
const metadata = {
customMetadata:{
'imageID': uuidv4(),
'User': auth.currentUser.email,
}
}
const uploadPicture = (e) => {
e.preventDefault();
const image = e.target[0].files[0];
const imageRef = ref(storage,'/images/'+user.uid+'/'+image.name)
const uploadTask = uploadBytesResumable(imageRef, image,metadata);
if (image) {
uploadTask.on("state_changed",
(snapshot) => {
console.log("Uploaded image");
console.log('current uid'+auth.currentUser.uid)
},
(error) =>
console.log('didnt upload '+error),
()=>{
getDownloadURL(uploadTask.snapshot.ref)
.then((url)=>{
set(ref(db,'users/'+auth.currentUser.uid+'/images/'),url);
console.log(url)
})
}
)
}
else {
console.log('Please Choose an image');
alert('image type not supported');
return;
}
};
return (
<div className="container">
<form onSubmit={uploadPicture}>
<input type="file" />
<button >Upload</button>
</form>
</div>
);
}
export default AddPicture;