I am trying to build a post system, similar to instagram's, where a user has the chance to upload a image or video that is then saved within a certain document's mediaURL field. The problem is: I want to display the image first to the user himself in the post form. Therefore I need to store the image first, the question is under what name should it be saved?
Normally I would create a post with an auto-generated ID and then save the image under that ID. I assume that there would be a better way that I could maybe generate an idea, that could then be given to save the post itself.
const post = await addDoc(collection(db, "posts"), {
text : text,
mediaURL : "",
author : currentUser.uid,
createdAt : firebase.firestore.FieldValue.serverTimestamp(),
likes : []
})
const fileRef = ref(storage, post.id);
const snapshot = await uploadBytes(fileRef, file);
const mediaURL = await getDownloadURL(fileRef);
const userRef = doc(db, "users", currentUser.uid)
await updateDoc(userRef, {
posts : arrayUnion(post.id)
})
const postRef = doc(db, "posts", post.id)
await updateDoc(postRef, {
mediaURL : mediaURL
})
Given the problem that I need to upload the image first, creating an empty post, that might exist for longer might be a problem. So under what name can I save the file?