【问题标题】:get Map data from Firestore从 Firestore 获取地图数据
【发布时间】:2020-08-29 12:31:51
【问题描述】:

我有一个带有对象的 localStorage 数组。然后我将这个数组添加()到 firestore.in 结果我有一个像这样的文档firestore document 我这里有 2 张地图。那么我怎样才能让它显示出来。当我使用此代码时,我只能显示时间戳

this.sells = this.sellsCollection.snapshotChanges().pipe(
      map(
        changes => {
          return changes.map(a => {
            const data = a.payload.doc.data() as Sell
            const id = a.payload.doc.id;
            return data
          })
        }
      )
    )

我可以在使用 | 时查看所有数据json管道。但看不到插值的地图数据

统一更新: console log of data

【问题讨论】:

  • 显示您的模板代码。
  • 很简单:第一个显示 json 中的所有数据
    {{sell | json}}
    ----second 只显示日期:
    {{sell.items}}, {{sell.date.seconds*1000 | date:"dd-mm-yyyy"}}
    sell.items 是我的界面中包含此项目的数组
  • 您发布的 Firestore 图像似乎没有 items 属性。您应该能够访问sell.stocksell.price 等...或者,也许,除了date 之外,其他属性将由数字索引(在类似样式的数组中)。我会试着写一个答案,你试试看。
  • 好的,我尝试了 sell.stock 结束其他属性,但一无所获。仅显示静止日期字段
  • 看看我的答案,试试看。

标签: angular typescript google-cloud-firestore angularfire


【解决方案1】:

在你的map observable 运算符中试试这个:

changes => {
  return changes.map(a => {
      const temp = a.payload.doc.data() as Sell

      // Each temp has the following shape (it looks like an array
      //   but it isn't really one. It's a more general object that
      //   is not an iterable out-of-the-box):
      // {
      //   1:    {...},
      //   2:    {...},
      //   3:    {...},
      //   ...
      //   date: '...'
      // }
      // We want to convert this object, to another object
      //   with the following shape: {items: any[], date: string}
      // To do that, what we can do is iterate over all of the keys
      //   of the incoming object (shown above) except for the 'date' 
      //   key and put all of their values in the items array. 
      // We can do that by getting all of the keys as an array
      //   (Object.keys) and iterate through them, filtering out the
      //   'date' key. For the other keys, that actually pass by the
      //   filter, we use the map function to turn them into their actual
      //   values.
      const items = Object.keys(temp)
          .filter(key => key !== 'date')
          .map(key => temp[key]);

      // Now we just have to build up the object to be returned, including
      //   the date, that was filtered out in the code above.
      return {items, date: temp.date};
    }) // end of Array.map
}

然后,在您的模板中,您可以:

<div *ngFor="let sell of sells">
  <div *ngFor"let item of sell?.items">
    {{item?.stock}}
  </div>
</div>

【讨论】:

  • 抱歉,最后一个问题。我想正确理解 .ts 中发生了什么。所以在 const 项目中,我们放置了文档的所有字段。然后,使用过滤器,我们得到所有不等于日期的字段 - 那是我们的项目。 .map 中发生了什么?
  • 在上面评论,在答案中。
猜你喜欢
  • 2019-09-30
  • 1970-01-01
  • 2021-01-06
  • 1970-01-01
  • 2020-12-14
  • 2019-07-03
  • 1970-01-01
  • 2018-10-21
相关资源
最近更新 更多