gpt4 book ai didi

javascript - 如何查询具有特定引用类型的 firebase 项目

转载 作者:行者123 更新时间:2023-11-30 19:46:50 31 4
gpt4 key购买 nike

我有一个父集合 categories 和它的子集合 directoriesDirectories 通过 Category 属性连接到 Categories

enter image description here

我想查询类别等于级别的所有目录

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

我得到一个空数组作为返回。你会如何解决这个问题?

更新 基于 answer由@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.不支持的字段值:自定义 AngularFirestoreDocument 对象

最佳答案

如果你想使用 DocumentReference查询中的数据类型,您必须构建一个 DocumentReference 并在查询中使用它,如下所示(在“标准”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 类型数据)在名为 的集合中目录


更新:以下内容似乎不适用于 angularFire2,请参阅评论

因此,如果我没记错的话,这将根据您问题的代码按以下方式完成:

const categoryDocRef = this.firestore.doc('categories/levels');

this.firestore
.collection<any>('directories', ref => ref.where('parent', '==', categoryDocRef))
.get()
...

关于javascript - 如何查询具有特定引用类型的 firebase 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54857939/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com