【问题标题】:Firestore - How create DocumentReference using path StringFirestore - 如何使用路径字符串创建 DocumentReference
【发布时间】:2018-11-16 06:56:52
【问题描述】:

Firebase 实时数据库允许使用创建引用

 var reference = db.ref(path);

firestore 中是否存在任何方法,以便我可以使用路径字符串创建文档引用。

如果方法存在,我如何在 android 中找到路径字符串,然后如何使用该路径在 node.js 中创建文档引用。

【问题讨论】:

    标签: android firebase google-cloud-firestore


    【解决方案1】:

    是的,您也可以在 Cloud Firestore 中实现这一点。所以这些是你的选择:

    FirebaseFirestore db = FirebaseFirestore.getInstance();
    

    第一个选项:

    DocumentReference userRef = db.collection("company/users");
    

    第二个选项:

    DocumentReference userRef = db.document("company/users");
    

    第三个选项:

    DocumentReference userRef = db.collection("company").document("users");
    

    【讨论】:

    • 这不是真的。您可以将路径传递给 FirebaseFirestore.document()。
    • 路径可以包含任意数量的集合和子集合。
    • @DougStevenson 对,但是你不能将路径传递给collection 方法,只能传递给文档,对吧?
    • 我认为 FirebaseFirestore 的变量名称应该类似于 db 或 firestore,因为 rootRef 不太容易混淆,因为它是数据库的对象而不是引用。
    • 刚刚把rootRef 改成了db,别搞混了。
    【解决方案2】:

    对于 web/javascript,db.doc() 将从字符串创建 DocumentReference:

    let docRef = db.doc(pathString)

    例如let userRef = db.doc('users/' + userId)

    【讨论】:

      【解决方案3】:

      您可以使用FirebaseFirestore.document() 并将其传递给您想要的文档的路径。每个文档必须位于一个集合中。如果您在名为 collectionId 的集合中查找名为 documentId 的文档,则路径字符串将为 collectionId/documentId

      【讨论】:

      • 谢谢,但我认为你不完全正确,我在 android studio 中试过没有直接方法 FirebaseFirestore.document() 首先我必须创建 firestore 的对象然后可以调用方法.....请编辑您的答案,以便我接受它
      • 当我说FirebaseFirestore.document() 时,我指的是在FirebaseFirestore 类型的对象上调用document() 的方法。您可以通过调用FirebaseFirestore.getInstance() 来获取FirebaseFirestore 类型的对象。
      • document() 不是静态方法,为了能够调用它,您需要先创建类的对象。 FirebaseFirestore rootRef = FirebaseFirestore.getInstance();.
      【解决方案4】:

      你只需要定义路径:

      `users/${type}/${user.uid}/`
      

      firestore
        .collection(`users/${type}/${user.uid}/`)
        .add(documentData)
        .then(function () {
          message = { message: "document added", type: ErrorType.success };
        })
        .catch(function (error) {
          message = { message: error, type: ErrorType.error };
        });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-27
        • 1970-01-01
        • 1970-01-01
        • 2019-06-19
        相关资源
        最近更新 更多