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

192
Views
Add document with auto-generated ID in sub collection?

I'm trying to add a document with auto-generated ID in reviews/something using:

await addDoc(collection(dbFirestore, "reviews", route.params.id), {
  rating: rating,
  text: text,
  user: {
    id: user.id,
    username: user.username,
    avatar: user.avatar,
  },
});

But I get:

Invalid collection reference. Collection references must have an odd number of segments, but reviews/XXX has 2.

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

0

Assume reviews is a collection and want to add a document (with auto generated id) to its subcollection

await addDoc(collection(dbFirestore, "reviews/document_id/subcollection"), {
  rating: rating,
  text: text,
  user: {
    id: user.id,
    username: user.username,
    avatar: user.avatar,
  },
});
about 4 years ago · Juan Pablo Isaza Report

0

As the error says, a CollectionReference must have odd number of path segments but you have 2. You just need to specify the collection name so try refactoring the code as shown below:

// i.e. remove route.params.id
await addDoc(collection(dbFirestore, "reviews"))
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!