我正在使用angularfirebase开发Ionic5-angular应用程序,并且在firestore数据库中有以下集合,其中伙伴是对用户的文档引用数组。我想显示所有公寓列表以及合作伙伴的名称(用户集合中的名字和姓氏)。当我使用其ID查询一间公寓并从其他集合中获取其他详细信息时,我可以编写查询以正确获取数据。但是,当我试图像在这种情况下那样为用户提供所有其他详细信息的公寓时,我正在努力。
当我有公寓ID时获取一间公寓的详细信息的方法:
getApartmentById(aptId: string) {
return this.afs.collection('Apartment').doc<apartment>(aptId).snapshotChanges()
.pipe(
switchMap(apart => {
return combineLatest(
of(apart),
combineLatest(
this.afs.collection<address>('Address', ref => ref.where('ApartmentID', '==', apart.payload.ref)).snapshotChanges()
.pipe(
map(addresses => {
const data = addresses[0].payload.doc.data() as address;
const id = addresses[0].payload.doc.id;
return { id, ...data };
})
)
),
combineLatest(
this.afs.doc<apartmentType>(apart.payload.data().Type).snapshotChanges()
.pipe(
map(atype => {
//console.log(atype.payload.id);
const data = atype.payload.data() as apartmentType;
const id = atype.payload.id;
return { id, ...data };
})
)
),
combineLatest(
this.afs.collection<phone>('Phone', ref => ref.where('ApartmentID', '==', apart.payload.ref)).snapshotChanges()
.pipe(
map(phones => {
const data = phones[0].payload.doc.data() as phone;
const id = phones[0].payload.doc.id;
return { id, ...data };
})
)
)
)
}),
map(([apart, add, room, phn]) => {
//console.log(add);
return {
apartment: apart.payload.data(),
address: add[0],
apartmenttype: room[0],
phone: phn[0]
}
})
);
}
有人可以帮助我如何获取所有公寓的详细信息,并加入用户以获得名字。我希望紧随其后。
[
{
apartmentName: "Some value",
partners:[
{firstname:"some value", lastname:"some value"},
{firstname:"some value", lastname:"some value"}
]
},
{
apartmentName: "Some value",
partners:[
{firstname:"some value", lastname:"some value"},
{firstname:"some value", lastname:"some value"}
]
},
{
apartmentName: "Some value",
partners:[
{firstname:"some value", lastname:"some value"},
{firstname:"some value", lastname:"some value"}
]
}
]
答案 0 :(得分:0)
使用这两个功能应该可以实现它。
这个想法是使用与您拥有的函数相似的函数,该函数获取每个单元,第二个函数将所有单元的引用存储到数组中,并使用map将第一个函数应用于该数组的所有元素
getApartment(apt: DocumentReference) {
return apt.snapshotChanges()
.pipe(
switchMap(apart => {
return combineLatest(
of(apart),
combineLatest(
this.afs.collection<address>('Address', ref => ref.where('ApartmentID', '==', apart.payload.ref)).snapshotChanges()
.pipe(
map(addresses => {
const data = addresses[0].payload.doc.data() as address;
const id = addresses[0].payload.doc.id;
return { id, ...data };
})
)
),
combineLatest(
this.afs.doc<apartmentType>(apart.payload.data().Type).snapshotChanges()
.pipe(
map(atype => {
//console.log(atype.payload.id);
const data = atype.payload.data() as apartmentType;
const id = atype.payload.id;
return { id, ...data };
})
)
),
combineLatest(
this.afs.collection<phone>('Phone', ref => ref.where('ApartmentID', '==', apart.payload.ref)).snapshotChanges()
.pipe(
map(phones => {
const data = phones[0].payload.doc.data() as phone;
const id = phones[0].payload.doc.id;
return { id, ...data };
})
)
)
)
}),
map(([apart, add, room, phn]) => {
//console.log(add);
return {
apartment: apart.payload.data(),
address: add[0],
apartmenttype: room[0],
phone: phn[0]
}
})
);
}
getAllApartments(){
return this.afs.collection('Apartment').listDocuments().then({docsRef =>
return docsRef.map(getApartment());
});
});
}