简体   繁体   中英

Get field from Firestore document

Is it possible to extract and parse just a single field from a Firestore object without running a For Each or alternatively first passing this to an array?

Specifically, I have pulled a single document as below (below working):

const docRef = admin
      .firestore()
      .collection("profiles")
      .doc(profileId);

From this I would like to parse a single field (accountBalance) but not able to do this (assumed to work but does not):

const accountBalance = docRef.accountBalance;

Does this need to be parsed?

Is it possible to extract and parse just a single field from a Firestore object without running a For Each

Yes, it is possible. According to the official documentation, you can achieve this using DocumentSnapshot's get() function:

Retrieves the field specified by fieldPath. Returns undefined if the document or field doesn't exist.

In code, should look like this:

docRef.get().then(function(doc) {
    if (doc.exists) {
        console.log("profileId: ", doc.get("profileId"));
    } else {
        console.log("No such document!");
    }
}).catch(function(error) {
    console.log("Error getting document:", error);
});

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