I have an Angular app with a Home page where I show the 4 latest rows in the "transactions" Firebase collection (ordered by date, descending). Then there's a separate Transactions page where I show the top 10 rows in this collection (ordered by amount, descending). However, when I start on the Home page and then go to the Transactions page, in my bar chart which should show the top 10 transactions by amount, I still see the 4 most recent transactions from the Home page.
Demo link: https://tickrs-app.web.app/
Steps to reproduce:
- open the demo app
- on the home page at the very bottom, you will see "Recent transactions"
- open the menu and navigate to the "Transactions" page
- the bar chart will look a bit strange, it seems the data still contains the 4 recent transactions from the home page
- navigate to a different page (not the Home page) and then back to the "Transactions" page, the bar chart should look normal now
Here's my code for the home.page.ts:
// Function to load the 4 most recent transactions to show on the home page
async loadData() {
// Order by date, descending
const orderParamsDateDesc = {
field: 'date',
order: 'desc'
}
// Call our service to load the data, given the ordering details, and limit the number of rows to 4
await this._FirebaseService.readSortLimit('transactions', orderParamsDateDesc, 4).then(result => this.transactionRows = result);
}
async ngOnInit() {
// Only try to load the data if the user is authenticated again
this.afAuth.onAuthStateChanged(async () => {
await this.loadData();
})
}
Here's the same code for the transaction.page.ts:
// Function to load the top 10 transactions, ordered by amount (descending)
async getRows() {
// Initialize the arrays
this.barChartDataEur = [];
this.barChartLabelsEur = [];
let rows: any = [];
// Order by amount, descending
let orderParams = {
field: 'amount',
order: 'desc'
}
// Call our service to load the data given the ordering details, and limit the number of rows to 10
await this._FirebaseService.readSortLimit("transactions", orderParams, 10).then(result => rows = result);
// Loop over the resulting rows and load the stock tickers and amount separately in the arrays which will be used for the bar chart
await rows.forEach(row => {
this.barChartLabelsEur.push(row.ticker.slice(0, 8));
this.barChartDataEur.push(row.amount);
});
// Set the loaded flag to true
this.loaded = true;
}
ngOnInit() {
// Only execute this part if user is authenticated
this.afAuth.onAuthStateChanged(async () => {
this.getRows();
})
}
Here's part of the transaction.page.html to render the bar chart:
<div class="chart-canvas">
<canvas baseChart *ngIf="loaded" // Only if data is loaded
[data]="barChartDataEur"
[labels]="barChartLabelsEur"
[chartType]="barChartType"
[options]="barChartOptions"
[colors]="barChartColors"
[legend]="barChartLegend"
[plugins]="barChartPlugins">
</canvas>
</div>
Here is my firebase.service.ts with the readSortLimit function that is used on both pages:
// Input: name of the Firebase collection, the ordering details and the number of rows to return
readSortLimit(collection, orderDetails, limitNumber) {
return new Promise((resolve, reject) => {
let result = [];
this.firestore
.collection(collection, ref => ref
.orderBy(orderDetails.field, orderDetails.order)
.limit(limitNumber)
)
.snapshotChanges()
.subscribe(item => {
Array.from(item).forEach(row => {
result.push(row.payload.doc.data());
});
resolve(result);
});
});
}