After looking at this answer I tried to use async/await with a firestore call but I think I am missing something.
I am trying to use Snapshot to get a collection of 'hex' documents for a hex grid. (I had valueChanges working but then realized I'm going to need metadata) I want to get the hexes, then sort them into rows, and finally return them to the component. I can see that it is returning before the snapshot and pipe actions are completed as it is empty "hexRows['test']" being console logged out as "true".
TS:
Service:
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
}
Component
hexRows: any
constructor(public dialog: MatDialog, public afs: FirestoreService) { }
ngOnInit() {
this.hexRows = this.afs.getHexesInRows()
}