Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

254
Views
How to save an image that is assigned to a certain document

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?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!