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

116
Views
How do I changed this Firebase version 8 syntax to version 9?

This is the syntax I originally used to add a document to a subcollection "posts". But I think it is not supported by firebase version 9

const unsubscribe = db.collection("users").doc(firebase.auth().currentUser.email).collection("posts").add({
  imageUrl: currentLoggedInUser.unsername,
  user: CurrentLoggedInUser.username,
  profile_picture: CurrentLoggedInUser.profilePicture,
  owner_uid: auth.currentUser.uid,
  caption: caption,
  createdAt: serverTimestamp(),
  likes: 0,
  likes_by_users: [],
  comments: [],
}).then(() => navigation.navigate('HomeScreen'))
return unsubscribe

What I tried

const unsubscribe = (doc(collection(db, "users")), auth.currentUser.email, addDoc(collection("posts"), {
  imageUrl: imageUrl,
  user: CurrentLoggedInUser.username,
  profile_picture: CurrentLoggedInUser.profilePicture,
  owner_uid: auth.currentUser.uid,
  caption: caption,
  createdAt: serverTimestamp(),
  likes: 0,
  likes_by_users: [],
  comments: [],
}).then(() => navigation.navigate('HomeScreen'))
return unsubscribe

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Things become much more readable if you capture the result of some of these operations in a meaningfully named variable.

const usersRef = collection(db, "users");
const userRef = doc(usersRef, auth.currentUser.email);
const postsRef = collection(userRef, "posts");
const promise = addDoc(postsRef, {
  imageUrl: imageUrl,
  user: CurrentLoggedInUser.username,
  profile_picture: CurrentLoggedInUser.profilePicture,
  owner_uid: auth.currentUser.uid,
  caption: caption,
  createdAt: serverTimestamp(),
  likes: 0,
  likes_by_users: [],
  comments: [],
});
return promise.then(() => navigation.navigate('HomeScreen'));

Once the above works, you can rewrite the first three lines to:

const postsRef = collection(db, "users", auth.currentUser.email, "posts");
about 4 years ago · Juan Pablo Isaza Report
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!