【问题标题】:I need to get an array instead of an object from a firestore collection我需要从 firestore 集合中获取数组而不是对象
【发布时间】:2019-06-24 14:10:45
【问题描述】:

我开始将 Angular 材料与 cloud-firestore 一起使用,但在数据来源和表的数据源方面存在以下问题。问题是对firestore的查询返回一个带有索引的对象,而MatTableDataSource需要一个数组,我无法将返回查询的对象转换为数组。 我尝试使用循环和 .map 运算符来填充一个空数组,但我总是创建一个带有索引的对象数组,而不是一个简单的对象数组。

这是我的数据库:

Collection that I'm querying

我的服务:

findpagosCliente(pagosId: number[]) {
    const col = this.afs.collection<Cliente>('pagoscliente')
    const queries = pagosId.map(el => col.doc(el.toString()).valueChanges());
    return combineLatest(...queries);    
}

我的组件:

inicializarTablaPagos() {
this.clienteService.findpagosCliente(this.clienteSeleccionado.facturacion.pagos).subscribe((pagos) => {   
      this.dataSource = new MatTableDataSource(pagos); 
      this.dataSource.paginator = this.paginator;
      this.showClientData = true;
    })
}

MatTableDataSource 等待一个数组,但 angularfire2 查询将我发回,对象如下: object from firestore

我尝试了很多循环和运算符来尝试创建一个新的简单数组,但它们最终都生成了一个新数组,就像上图中的那样。例如:

this.clienteService.findpagosCliente(this.clienteSeleccionado.facturacion.pagos)
      .subscribe((pagos) => {   
        let arraytemp = [];     
        pagos.map(element => {
          arraytemp.push(element)
        })
        this.dataSource = new MatTableDataSource(arraytemp); 
        this.dataSource.paginator = this.paginator;
        this.showClientData = true;
      })

我知道我做错了什么,而且很明显,但现在我看不到它。

感谢您的回答

【问题讨论】:

    标签: angular google-cloud-firestore angular-material2


    【解决方案1】:

    你得到的结果已经是一个数组。看看你图片中的符号:[{...}, {...}] 方括号始终是数组的指示符。

    【讨论】:

    • 问题出在表中列的定义,引用和名称不匹配,非常感谢!!!
    • 你是说字段名吗?
    猜你喜欢
    • 2018-07-13
    • 1970-01-01
    • 2019-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-30
    • 2020-03-28
    相关资源
    最近更新 更多