Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get document data from firebase.firestore.DocumentSnapshot?

I get document from Firestore, it's work:

this.sub = this.beluginService.getBeluginById(this.personId)
.subscribe((documentSnapshot: firebase.firestore.DocumentSnapshot) => {
  console.log(documentSnapshot.data());
}

My service:

  getBeluginById(id: string) {
    return this.afs.collection('belugin').doc(id).get();
  }

In console.log I get one object:

{ id: "ya1jibU2pZx1niGiuAmp"
  qwCF: [0, 0, 2, 0, 0, 6, 2]
  qwCO: [0, 0, 0, 0, 2, 0, 0]
  qwIMP: [10, 0, 2, 2, 2, 4, 0]
  qwME: [0, 4, 0, 4, 2, 0, 0]
  qwPL: [0, 0, 0, 0, 0, 0, 0]
  qwRI: [0, 2, 2, 2, 2, 0, 0]
  qwSH: [0, 0, 0, 0, 0, 0, 0]
  qwTW: [0, 4, 4, 2, 2, 0, 8] }

But when I try to get object properties (etc id, qwCF):

this.sub = this.beluginService.getBeluginById(this.personId)
.subscribe((documentSnapshot: firebase.firestore.DocumentSnapshot) => {
  this.id = documentSnapshot.data().id;
  this.qwCF = documentSnapshot.data().qwCF;
}

I can't get data() properties.... My VCCode shows a method data() call error. Why?

like image 685
Вячеслав Avatar asked Sep 13 '25 04:09

Вячеслав


1 Answers

If documentSnapshot.data() work in the log maybe takind the data from it it will be better:

const data = documentSnapshot.data();
this.id = data.id

Depending on the ECMA version maybe

this.id = data["id"];
like image 160
cutiko Avatar answered Sep 15 '25 19:09

cutiko