【问题标题】:Get firebase object with Angular使用 Angular 获取 firebase 对象
【发布时间】:2021-02-16 16:18:43
【问题描述】:

我正在尝试使用 Angular 获取 firebase 对象,我成功地做到了。但是,我不确定如何更深入地了解返回的内容(下图)。

What is returning nowMy Firebase object

我是这样来的:

客户服务

getClientsOfThisBranch(branchNumber:string){
   return this.db.list('/clients/' + branchNumber).snapshotChanges();
}

一些组件

clientInfo$: any[] = [];

this.clientService.getClientsOfThisBranch('17').subscribe(data => {
   data.forEach(x => { console.log( x.payload.val() ); this.clientInfo$.push(x.payload.val())})
});

任何帮助将不胜感激。 谢谢。

【问题讨论】:

    标签: angular typescript firebase


    【解决方案1】:

    从您的问题中我可以理解,您是否正在尝试将返回的数据转换为 clientInfo 数组。您将数据作为包含客户信息的客户 ID 的对象。可以使用Object.keys()Object.values()进行更深的遍历。

    clientInfo: any[];
    
    this.clientService.getClientsOfThisBranch('17').subscribe(data => {
      this.clientInfo = data.map(x => { 
        let obj = x.payload.val();
        let id = Object.keys(obj)[0];  //Assuming only 1 item as per image posted
        let info = Object.values(obj)[0];   //Assuming only 1 item as per image posted
        console.log(id, info);
      })
    });
    

    如果你有多个值,你可以迭代它们。

    【讨论】:

      猜你喜欢
      • 2017-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-19
      • 2019-05-31
      • 2022-01-16
      • 1970-01-01
      • 2022-08-17
      相关资源
      最近更新 更多