简体   繁体   中英

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

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

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)
));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM