代码之家  ›  专栏  ›  技术社区  ›  NickP

从Firestore文档获取字段

  •  0
  • NickP  · 技术社区  · 6 年前

    有没有可能只从Firestore对象中提取和解析一个字段而不运行For-Each或者先将其传递给数组?

    具体来说,我提取了一个文档,如下所示(如下所示):

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

    因此,我想解析一个字段(accountBalance),但无法执行此操作(假定可以工作,但不能):

    const accountBalance = docRef.accountBalance;
    

    这需要解析吗?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Alex Mamo    6 年前

    有没有可能只从Firestore对象中提取和解析一个字段而不运行For Each

    是的,这是可能的。根据官方文档,您可以使用DocumentSnapshot的 get()

    在代码中,应该如下所示:

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