【问题标题】:Firebase get document collection based on specific field using angularFirebase使用角度获取基于特定字段的文档集合
【发布时间】:2021-11-16 16:51:24
【问题描述】:

我想根据 UserId 字段获取集合数据。

getData(){
   const data$ = this.fbs.collection(this.trackerData,ref=> ref.where('UserId','==',"WrKDk0XSNjU20FI0EVRvADzvNHz1")).snapshotChanges().subscribe(res=>{
     console.log(res);
   });
}

如果我得到这样的数据,我得到的响应是:-

但我需要响应数据,例如:-

【问题讨论】:

  • DocumentSnapshot 对象位于第一个屏幕截图结果的 payload 属性中,因此您应该能够从那里获取它们。

标签: javascript firebase ionic-framework google-cloud-firestore angularfire2


【解决方案1】:

你需要转换你的流以获得你想要的:

getData(userId: string){
    return this.fbs.collection(
        this.trackerData,
        ref=> ref.where('UserId','==',userId)
    ).snapshotChanges().pipe(
        map(action => {
            const data = action.payload.data();
            const id = action.payload.id;
            return { id, ...data };
        })
    );
}

getData("WrKDk0XSNjU20FI0EVRvADzvNHz1").subscribe(res=> console.log(res));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-20
    • 2021-07-04
    • 2021-07-20
    • 1970-01-01
    • 2021-01-03
    • 1970-01-01
    • 2022-01-19
    • 2019-02-26
    相关资源
    最近更新 更多