问题
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