0

I am currently encountering this problem in my react native app using Firebase:

Collection references must have an odd number of segments

I've seen similar cases in stackoverflow but wasn't able to solve my problem. Here is my code :

  const getData = async () => {
    console.log(user.uid + " ")
    const col = collection(db, "users", user.uid)
    const taskSnapshot = await getDoc(col)

    console.log(taskSnapshot)
  }

  getData()

I am trying to open my document with the document reference (user.uid) but I am getting this error : Collection references must have an odd number of segments

Hope you can help me solve this problem.

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
owdji
  • 151
  • 2
  • 12

2 Answers2

1

The getDoc() takes a DocumentReference as parameter and not a CollectionReference so you must use doc() instead of collection().

import { doc, getDoc } from "firebase/firestore" 

const docRef = doc(db, "users", user.uid)
const taskSnapshot = await getDoc(col)
console.log(taskSnapshot.data() || "Doc does not exists")

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

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
0

replace this

collection(db, "users", user.uid)

with this

collection(db).document("users").collection(user.uid)
Sachila Ranawaka
  • 39,756
  • 7
  • 56
  • 80