I'm using the onSnapshot to get realtime updates in the database.
I get the data for the table using this code:
Service.ts
export class ExtractorService {
constructor(private fstore: AngularFirestore) { }
sampleFunction() {
return new Promise(resolve => {
this.fstore.firestore.collection('banned').doc('words')
.onSnapshot(function(doc) {
//console.log("from service (WORDS): ", doc.data().cars)
resolve(doc.data().cars);
});
});
}
To get the data on the service I use this code for the Component.ts
export class sampleComponent implements OnInit {
banWords = [];
constructor(private extractorService: ExtractorService) {}
ngOnInit() {
this.loadBannedWords()
}
loadBannedWords(){
this.extractorService.sampleFunction().then(data => {
this.banWords = (data as unknown as string[]);
console.log("words: ", this.banWords )
})
}
Then I load the table using this HTML:
<tr *ngFor ="let item of banWords ; index as i ">
<td>{{ i+1 }}</td>
<td>{{ item }}</td>
Then my database is look like this:
The issue here is when I add,update or delete data on the database, the table was not updated or reloaded automatically. I need to refresh the page in order for the table to be updated.
How can I make the table update when the database was updated?
