Database.list callback?
Here is my code:
export class DashboardComponent {
private items: FirebaseListObservable<Array<any>>;
constructor(private fire: AngularFire) {
this.items = this.fire.database.list('items');
}
}
Is there a way i can have a then/catch callback so i can prevent the error (permission_denied at /items: Client doesn't have permission to access the desired data) from showing in the console when the user is not yet authenticate?
Thanks
Assuming you're using an async pipe, you should check authentication before binding this to the view. One way to do this in the template would be:
<div *ngIf="fire.auth | async">
<span *ngFor="let item of items | async"></span>
</div>
Otherwise, in your component you could add a .catch to the observable, or subscribe to the items observable inside the template and provide an error handler instead of using async pipe.
Hi @jeffbcross, but what about if you just want to listen to when the data transfer is finished? If the user has a very slow connection I want to have something indicating that the data is still being fetched. How would I be able to listen to when the data fetching is done?
i think that
this.fire.database.list('items').subscribe(resp => console.log(resp););
should do the trick!
Hi @cindoum thank you for your quick reply, but unfortunately adding the subscribe part to my list causes the following error:
Error TS2322: Type 'Subscription' is not assignable to type 'FirebaseListObservable<any>'.
My code is the following:
export class HomePage {
todoList: FirebaseListObservable<any>;
constructor(public angularFire: AngularFire, public fireBaseAuth: FirebaseAuth, private popupProvider: PopupProvider, public navCtrl: NavController) {}
ngOnInit() {
this.fireBaseAuth.subscribe( // sincronize the item list with the backend
data => {
if (data) {
this.todoList = this.angularFire.database.list('/todoList').subscribe(resp => {
console.log(resp);
});
}
});
}
}
What am I doing incorrectly?
do not assign the result of the subscribe to your this.todoList. If you use the subscribe, you should do something like this (not tested).
this.angularFire.database.list('/todoList').subscribe(resp => { this.todoList.push(resp); });
@cindoum Thank you very much, that solved my issue.