正在从firebase检索子集合



我正在尝试使用Angular从firestore数据库中检索子集合。我有包含字段"Name"one_answers"Id"的集合"Company",以及包含字段"Name'"one_answers"Id'的子集合"CustomerList">

要检索公司集合,我有代码:

private companyCollection: AngularFirestoreCollection<Company>;
getCompany() {
return this.company= 
this.companyCollection.snapshotChanges().pipe(
map(changes => {
return changes.map(a => {
const data = a.payload.doc.data() as Company;
return data;     
});

我的问题是如何检索"CustomerList"子集合并将其添加到"Company"对象中。

这里有两件事:

1如果您的收藏是类似对象的:

在检查之前,您需要定义客户和公司之间的关系。

interface customer {
id: any;
name: string;
}
interface Company { 
id: any;
name: string;
customerList: customer[];
}

这里有一个非常简单的例子:

const customers = [
{
id: 0,
name: 'lilu',
},
{
id: 1,
name: 'lilu1',
},
{
id: 2,
name: 'lilu2',
},
{
id: 3,
name: 'lilu3',
}
];
const companies = [
{
id: 0,
name: 'lilu Company',
},
{
id: 1,
name: 'lilu1 Company',
},
{
id: 2,
name: 'lilu2 Company',
},
{
id: 3,
name: 'lilu3 Company',
},
];
let customersWithCompanies = [];
for (let item of companies) {
let obj = {
id: item.id,
name: item.name,
customerList: customers
};
customersWithCompanies.push(obj);
}
console.log(customersWithCompanies);

如果这是收集原型,你需要js/ts函数来获取,设置特定的数据,然后推送到firebase;

2你必须在firebase中收集不同的收藏品,它们彼此相连(Mayby在Idea级别(

在这种情况下,您需要从不同的集合中获取数据,这里是文档中代码的一部分。

从firebase文档中,您需要这样定义它:

import { doc } from "firebase/firestore"; 
const alovelaceDocumentRef = doc(db, 'users/alovelace');

请查看以下网址:https://firebase.google.com/docs/firestore/data-model#web-版本-9_2

若我并没有很好地理解这个主题,请给我一个答案,我会用例子把答案说得更清楚。

最新更新