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

187
Views
Firebase 9 - How to do new doc ref

Hello guys i've been trying to convert this code into modular firebase 9:

fb8: const userRef = db.collection('Users').doc();

to fb9: const userRef = doc(db, 'Users');

But i'm getting this error: FirebaseError: Invalid document reference. Document references must have an even number of segments, but Users has 1.

Please help!

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

0

The doc() method is equivalent to .collection('users').doc('docID') where you need to specify the ID. If you are trying to add a document with random ID then you add use addDoc() with collection() as shown below:

const usersCol = collection(db, 'Users')

await addDoc(usersCol, {...data})

If you want the random ID before adding the document then you can try this:

const userRef = doc(collection(db, 'Users'));
console.log(userRef.id)

Document references must have an even number of segments, but Users has 1.

You can checkout this answer for explanation of doc() and collection():

Firestore: What's the pattern for adding new data in Web v9?

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!