【发布时间】:2019-07-01 16:34:31
【问题描述】:
我要做什么列出属于每个客户的设备。
我已阅读、观看视频、复制和粘贴结果,但没有任何效果,我的头晕目眩。
我需要每个客户端和设备的 id,所以我像这样抓取它们
fetchJobs() {
this.todoCollectionRef = this.afs.collection('clients');
this.todo$ = this.todoCollectionRef.snapshotChanges().pipe(map(actions => {
return actions.map(action => {
const data = action.payload.doc.data() as Todo;
const id = action.payload.doc.id;
return {
id,
data
};
});
}));
}
fetchEquipment() {
this.equipmentCollectionRef = this.afs.collection('equipment');
this.equipment$ = this.equipmentCollectionRef.snapshotChanges().pipe(map(actions => {
return actions.map(action => {
const data = action.payload.doc.data() as Equipment;
const id = action.payload.doc.id;
return {
id,
data
};
});
}));
}
我也尝试重新考虑我的数据结构,但由于每个客户端都会有多个项目并且数量不一样,所以我尝试使用带有地图的数组但无法更新详细信息。
如果有人能指出正确的方向,我将不胜感激。
我想我需要这样的东西..
this.todoCollectionRef = this.afs.collection('clients');
this.todo$ = this.todoCollectionRef.snapshotChanges().pipe(map(actions => {
return actions.map(action => {
const data = action.payload.doc.data() as Todo;
const id = action.payload.doc.id;
return this.afs.collection('equipment', ref => ref.where('client', '==', data.client)).valueChanges()
.pipe(map(eqip => Object.assign({}, {id, data, eqip})));
});
})).switchMap(observables => Observable.combineLatest(observables));
}
If anyone can help here I would be truly grateful.
And if someone could explain the code to help me and others learn.
【问题讨论】:
-
你真正想要做什么?明确你的观点
-
我要做的是列出客户(已完成)然后查询属于该客户的设备的第二个集合并在同一页面上列出每个设备,所以我想我会通过客户名称查询,所以我的collection1.doc 将有名称、地址、电话号码等,然后collection2.doc 将有名称、品牌、型号、序列等。
-
我会在电脑前截屏,希望它能提供一个明确的目标。
-
尝试使用客户端 ID 存储设备,当您想要查询设备时,使用 .where() 函数根据客户端 ID 检索数据。如果您不知道如何使用 .where() 进行查询,我会为您发布答案。
-
我一直在尝试的是 asf.collection('equipment', ref => ref.where('clientname', '==' this.name));我正在做的是将客户端名称从客户端集合传递到设备一。这就是我被卡住的地方。如果我使用按钮将名称传递给设备,我可以做到这一点,但我正在寻找一种不通过按钮传递名称的方法。
标签: angular join collections google-cloud-firestore angularfire2