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

253
Views
ReactJS: add data to document when creating a subcollection

I need to created documents and subcollections of documents like this:

Structure

So here is my code:

function Subscriptions() {
  useEffect(() => {
    const fetchData = async () => {
      try {
        const userProjectsColRef = collection(db, "users");
        const newProjectDocRef = doc(userProjectsColRef);
        addDoc(collection(newProjectDocRef, "experiences"), {
          name: "hello",
        });
      } catch (err) {
        console.log(err);
      }
    };
    fetchData();
  }, []);
  return <div></div>;
}
export default Subscriptions;

How can I add data to the very first document created (the "data" field with "bio", "fullname", "company") ?

Right now, this code does add a subcollection named "experiences" which has a document with some data (name: "hello"). Like this:

enter image description here

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

0

You need to create two documents: firstly the parent user document and then
the experience document which DocumentReference is based on its parent doc's DocumentReference (i.e. newProjectDocRef).

const userProjectsColRef = collection(db, "users");
const newProjectDocRef = await addDoc(userProjectsColRef, {
  // ...
});
await addDoc(collection(newProjectDocRef, "experiences"), {
  // ...
});

Note that if you want those two writes to be done as an atomic operation, you should use a batched write.

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!