Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

499
Visualizações
Firebase - How can i get data from reference?

I have a firebase firestore with some reference datatype.

It looks like this: enter image description here

enter image description here

I Hope, this is the correct way.

Now when i get my user from firebase, i have this reference object with the id of it. But if i call my function to get the doc, i get an error message:

First my function and how i call it:

export const getClubById = async (id: string) => {
  const doc = collection(db, 'clubs', id)
  return doc
}
const userData = dbUser.data()
const club = await getClubById(userData.selectedClub.id)
console.log('club', club)

And here the error message:

Uncaught (in promise) FirebaseError: Invalid collection reference. Collection references must have an odd number of segments, but clubs/vA7R94pX3bpHDsYIr6Ge has 2.
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you have the DocumentReference already then you can use getDoc() function to retrieve the document from Firestore as shown below:

import { getDoc, DocumentReference } from "firebase/firestore";

export const getClubById = async (clubDocRef: DocumentReference) => {
  const clubSnapshot = await getDoc(clubDocRef);
  return clubSnapshot.data();
}

// Pass the reference itself to the function instead of doc ID
const club = await getClubById(userData.selectedClub)

For the error in the question, to create a DocumentReference, if you have the document ID then you should doc() function instead of collection() that is used to create a CollectionReferencce as shown below:

const docRef = doc(db, 'clubs', clubID);

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda