Get field from Firestore document

我与影子孤独终老i 提交于 2021-01-29 08:26:06

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/52702157/get-field-from-firestore-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!