javascript - 如何在 Firestore 中查询 DocumentReference

标签 javascript angular firebase google-cloud-firestore angularfire2

我有一组酒吧订单,在每个文档中我都有一个包含酒保引用的字段。

Illustrative image

我正在尝试使用文档引用来确定位置。

像这样的东西:

orders = this.database.collection("orders",
    (ref) => ref.where("barman.path", "==", "barmen/" + this.auth.auth.currentUser.uid),
);
return orders.valueChanges();
barman.path因为当我得到一个带有字段引用的文档时,这是获取引用路径的方法。

我已经尝试只使用 barman而是 barman.path .

我已经尝试使用 docref ( firestore.googleapis.com/pro... ) 的完整“路径”来创建 where。

有任何想法吗?

只放 id 而不是完整的引用将使系统的其他部分变得困难。

最佳答案

为了查询引用字段,您应该创建一个 DocumentReference与您尝试查询的位置匹配的对象,并使用它。请注意,这需要是原生 Firestore 引用,如 AngularFirestoreCollectionAngularFirestoreDocument只会作为“自定义对象类型”显示给 Firestore。您可以通过 ref 获取此信息AngularFirestore 类型的成员。

我不确定查询的“.path”部分来自何处,因为您要查询的实际字段是 barman .我已经在示例中删除了它。

在您的情况下,这将类似于:

const queryReference = this.database
    .collection('barmen')
    .doc(this.auth.auth.currentUser.uid).ref;

orders = this.database.collection('orders',
    (ref) => ref.where('barman', '==', queryReference),
);
return orders.valueChanges();

我显然无法完全测试它,因为没有足够的代码来显示 this.auth.auth.currentUser.uid在这种情况下有效。但它应该为您指明正确的方向。

关于javascript - 如何在 Firestore 中查询 DocumentReference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47608254/

相关文章:

angular - 动态更改 MatPaginatorIntl itemsPerPageLabel

firebase - Firestore 文档的最大字段数?

javascript - 与整个单词(包括重音字符)匹配的正则表达式

Angular 6 库作为 npm 包的替代品

angular - 为什么 Angular 4 没有检测到 NgFor 中 Array.Filter 的变化?

iOS 相册权限 nativescript

arrays - 在闭包中附加到数组

javascript - 得到一个未终止的字符串文字错误..我可以让它通过,但无法弄清楚为什么

javascript - 使用 JQuery 通过页面 URL 在页面中搜索值

javascript - 确定从 dragenter 和 dragover 事件中拖动的内容