简体   繁体   English

CollectionReference 到 DocumentReference

[英]CollectionReference to DocumentReference

I'm trying to perform a firestore transaction in Flutter. Therefore I need to pass the documentReference .我正在尝试在 Flutter 中执行 firestore 事务。因此我需要传递documentReference

There is no problem, when accessing the "lesson" document based on the identifier.根据标识符访问“课程”文档时没有问题。 The issue is when fetching the active user pass.问题是在获取活动用户通行证时。 The where I'm trying to use returns the Query , and the .reference() returns the CollectionReference (which is not accepted by the transaction method).我尝试使用的where返回Query.reference()返回CollectionReference (交易方法不接受)。

How can I get the reference to the document basing on the value of its field.如何根据其字段的值获取对文档的引用。

To spice it up: DB rules don't allow me to read ALL the passes.为它增添趣味:数据库规则不允许我阅读所有通行证。 I've got only access to those, that have my UserID (double checked - working).我只能访问那些具有我的用户 ID(双重检查 - 工作)的用户。

  DocumentReference lessonRef =
    await _db.collection('lessons').document(lesson.identifier);

  CollectionReference passRef = await _db
        .collection('passes')
        .where('userID', isEqualTo: user.identifier).reference();

数据库结构

You can get the DocumentReference of the Queried documents like so:您可以像这样获取查询文档的 DocumentReference:

Firestore.instance
.collection('passes')
.where('userID', isEqualTo: user.identifier)
.snapshots()
.listen((reference) =>
    data.documents.forEach((doc) => /*do what you want with doc.reference*/));

(I'm not an expert in Flutter and I could't test the code, but I based the answer in the API reference ) (我不是 Flutter 的专家,我无法测试代码,但我基于API 参考中的答案)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 预期 collection() 的第一个参数是 CollectionReference、DocumentReference 或 FirebaseFirestore - Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore Firestore v9 预期 collection() 的第一个参数是 CollectionReference、DocumentReference 或 FirebaseFirestore - Firestore v9 Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore 如何访问 firebase FirebaseError:预期 collection() 的第一个参数是 CollectionReference、DocumentReference 或 FirebaseFirestore - How can I access firebase FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore 有人可以帮我吗 - FirebaseError:collection() 的第一个参数应该是 CollectionReference、DocumentReference 或 FirebaseFirestore? - Can someone help me - FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore? Firestore“参考”与“文档参考” - Firestore "Reference" versus "DocumentReference" Firestore DocumentReference 类型检查不匹配 - Firestore DocumentReference Type Checking mismatch AttributeError: 'CollectionReference' 对象没有属性 'doc' - AttributeError: 'CollectionReference' object has no attribute 'doc' firestore:用户 Array.includes 和 DocumentReference - firestore: user Array.includes with a DocumentReference 如何从 Firebase `get()` 中获取 `DocumentReference` - How to fetch a `DocumentReference` from a Firebase `get()` 如何在 cloud firestore 的 CollectionReference 中添加 DocumentID? 使用 flutter - How can i add DocumentID in CollectionReference of cloud firestore? Using flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM