Home > database >  How to get only one field from firestore query, i am getting all the fields
How to get only one field from firestore query, i am getting all the fields

Time:09-19

I can output all the fields in console, but I want to output only 1. I only want to get the ID field but I somehow cannot do it. The following screenshots shows my code and the output.

Code:

this.afs.collection("accounts",ref=>ref
          .where("email","==", email)) // email of currently logged in
          .get()
          .subscribe(data=>
            data.forEach(element=>
              //console.log(el.data())
              console.log(element.data())
          ));

Output in console:

{id: '864b7TECyqUdd8Ql7f81saSgoOx2', lastName: '******',
address: '******', contactNo: '******', role: 
'******', …}
address: "******"
contactNo: "******"
email: "******"
firstName: "******"
id: "864b7TECyqUdd8Ql7f81saSgoOx2"
lastName: "******"
role: "******"
[[Prototype]]: Object

CodePudding user response:

Firestore always retrieves the complete document from the server, but you can display just one field with:

data.forEach(element=>
  console.log(element.data().id)
));
  • Related