[英]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? 是否可以从Firestore对象中提取和解析单个字段而无需运行For Each,或者先将其传递给数组?
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): 由此,我想解析一个字段(accountBalance),但无法做到这一点(假设可以工作,但不能):
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 是否可以从Firestore对象中提取和解析单个字段而无需运行For Each
Yes, it is possible. 对的,这是可能的。 According to the official documentation, you can achieve this using DocumentSnapshot's get() function: 根据官方文档,您可以使用DocumentSnapshot的get()函数实现此目的:
Retrieves the field specified by fieldPath. 检索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);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.