如何查询具有特定引用类型的 firebase 项目
How to query firebase items with specific reference type
我有一个父集合 categories
和一个子集合 directories
Directories
通过 Category
属性
连接到 Categories
我想查询类别等于level
的所有目录
this.firestore
.collection<any>('directories', ref => ref.where('categories', '==', 'levels'))
.get()
.pipe(
map(x => {
const out: [] = [];
x.forEach(y => {
out.push(y.data());
});
return out;
})
);
我在 return 中得到一个空数组。你会如何解决这个问题?
UPDATE 基于@renaud-tarnec 提供的 :
const categoryDocRef = this.firestore.doc('categories/levels');
this.firestore
.collection<any>('directories', ref => ref.where('categories', '==', categoryDocRef))
.get()
.pipe(
map(x => {
const out: [] = [];
x.forEach(y => {
out.push(y.data());
});
return out;
})
);
现在有一个错误core.js:15713 ERROR Error: Function Query.where() called with invalid data. Unsupported field value: a custom AngularFirestoreDocument object
如果要在查询中使用 DocumentReference 数据类型,则必须构建一个 DocumentReference 并在查询中使用它,如下所示(in "standard" JavaScript) :
const categoryDocRef = firebase.firestore().doc('categories/levels');
firebase.firestore().collection("directories").where("parent", "==", categoryDocRef)
.get()
.then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
console.log(doc.id, " => ", doc.data());
});
})
.catch(function(error) {
console.log("Error getting documents: ", error);
});
我假设包含字段 parent
的文档(反过来又包含 DocumentReference
类型的数据)在名为 directories
的集合中。
更新:以下内容似乎不适用于 angularFire2,请参阅评论
因此,如果我没记错的话,这将根据您问题的代码按照 angular 中的方式完成:
const categoryDocRef = this.firestore.doc('categories/levels');
this.firestore
.collection<any>('directories', ref => ref.where('parent', '==', categoryDocRef))
.get()
...
我有一个父集合 categories
和一个子集合 directories
Directories
通过 Category
属性
Categories
我想查询类别等于level
this.firestore
.collection<any>('directories', ref => ref.where('categories', '==', 'levels'))
.get()
.pipe(
map(x => {
const out: [] = [];
x.forEach(y => {
out.push(y.data());
});
return out;
})
);
我在 return 中得到一个空数组。你会如何解决这个问题?
UPDATE 基于@renaud-tarnec 提供的
const categoryDocRef = this.firestore.doc('categories/levels');
this.firestore
.collection<any>('directories', ref => ref.where('categories', '==', categoryDocRef))
.get()
.pipe(
map(x => {
const out: [] = [];
x.forEach(y => {
out.push(y.data());
});
return out;
})
);
现在有一个错误core.js:15713 ERROR Error: Function Query.where() called with invalid data. Unsupported field value: a custom AngularFirestoreDocument object
如果要在查询中使用 DocumentReference 数据类型,则必须构建一个 DocumentReference 并在查询中使用它,如下所示(in "standard" JavaScript) :
const categoryDocRef = firebase.firestore().doc('categories/levels');
firebase.firestore().collection("directories").where("parent", "==", categoryDocRef)
.get()
.then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
console.log(doc.id, " => ", doc.data());
});
})
.catch(function(error) {
console.log("Error getting documents: ", error);
});
我假设包含字段 parent
的文档(反过来又包含 DocumentReference
类型的数据)在名为 directories
的集合中。
更新:以下内容似乎不适用于 angularFire2,请参阅评论
因此,如果我没记错的话,这将根据您问题的代码按照 angular 中的方式完成:
const categoryDocRef = this.firestore.doc('categories/levels');
this.firestore
.collection<any>('directories', ref => ref.where('parent', '==', categoryDocRef))
.get()
...