【发布时间】:2019-07-08 01:47:47
【问题描述】:
查看this 答案后,我尝试将 async/await 与 firestore 调用一起使用,但我认为我遗漏了一些东西。
我正在尝试使用快照来获取十六进制网格的“十六进制”文档集合。 (我有 valueChanges 工作,但后来意识到我将需要元数据)我想获取十六进制,然后将它们排序成行,最后将它们返回给组件。我可以看到它在快照和管道操作完成之前返回,因为它是空的“hexRows ['test']”被控制台注销为“true”。
TS:
服务:
async getHexesInRows(){
let hexRows = {test: true}
this.hexCollection = this.db.collection('hexes', ref=> ref.orderBy('id').limit(5))
this.hexes = await this.hexCollection
.snapshotChanges()
.pipe(
map(actions => actions.map( hex => {
const data = hex.payload.doc.data()
const uid = hex.payload.doc.id
return {uid, ...data}
})),
map(data => {
data.forEach((hex, index) => {
let rowNum = Math.floor(index / 10)
hexRows = {test: false}
if(hexRows[`row${rowNum}`] == undefined){
hexRows[`row${rowNum}`] = []
}
hexRows[`row${rowNum}`].push(hex)
})
return data
})
)
console.log('Rows', hexRows);
console.log('Hexes', this.hexes);
return hexRows
}
组件
hexRows: any
constructor(public dialog: MatDialog, public afs: FirestoreService) { }
ngOnInit() {
this.hexRows = this.afs.getHexesInRows()
}
【问题讨论】:
标签: angular firebase async-await rxjs google-cloud-firestore