When uploading an image in Storage, I want to be able to get the fullPath or an imageURL and save it under my 'Articles' documents in Cloud Firestore. So basically, I have a form with a 'title' and the upload image should be a part of it. However, I don't know how to make each image know to which article it belongs to. This is what I have so far but I'm not sure it's fine. I only manage to save the images in Storage, nothing else.
What do I do so that the image knows to which article it belongs to?
function UploadFile(props) {
const { documentId } = props;
const [fileUrl, setFileUrl] = React.useState(null);
const onFileChange = async (e) => {
const file = e.target.files[0];
const storageRef = firebase.storage().ref();
const fileRef = storageRef.child(file.name);
await fileRef.put(file);
if (documentId) {
setFileUrl(
await fileRef.getDownloadURL().then((fileUrl) => {
firebase
.firestore()
.collection("articles")
.doc(documentId)
.update({
fileUrl: fileUrl,
})
.then(() => {
setFileUrl("");
});
})
);
}
};