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

137
Visualizações
Google firestore to get single record. but not able to cast to object

This is my controller class. where i want to convert data to my custom object

getRegister(node:string) {
   
    this.db.firestore.collection('databasetable')
        .where('profileId','==', node)
        .get()
        .then(querySnapshot => {
                querySnapshot.forEach(function (doc) {
                      console.log(doc.id); // id of doc
                      console.log(doc.data()); // data of doc
                      //Want to cast my object here for displaying into UI
                })
         });
         
  }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The data() method always returns a JSON object. I think you're looking for the (relatively recent) withConverter methods and FirestoreDataConverter class:

await firebase.firestore()
  .collection('posts')
  .withConverter(postConverter) // 👈 this is a custom class that performs the conversion
  .doc().get();

The postConverter above is an object that has toFirestore and fromFirestore methods to convert the JSON from/to the custom class, for example:

const postConverter = {
  toFirestore(post: Post): firebase.firestore.DocumentData {
    return {title: post.title, author: post.author};
  },
  fromFirestore(
    snapshot: firebase.firestore.QueryDocumentSnapshot,
    options: firebase.firestore.SnapshotOptions
  ): Post {
    const data = snapshot.data(options)!;
    return new Post(data.title, data.author);
  }
};

Also see the documentation for the DocumentReference.withConverter method and the FirestoreDataConverter class, where I got the above code samples from.

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