angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

Database.list callback?

Open cindoum opened this issue 10 years ago • 6 comments

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

cindoum avatar Jul 12 '16 03:07 cindoum

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.

jeffbcross avatar Jul 13 '16 20:07 jeffbcross

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?

LuisMCunha avatar Aug 31 '16 11:08 LuisMCunha

i think that

this.fire.database.list('items').subscribe(resp => console.log(resp););

should do the trick!

cindoum avatar Aug 31 '16 11:08 cindoum

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?

LuisMCunha avatar Aug 31 '16 13:08 LuisMCunha

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 avatar Aug 31 '16 13:08 cindoum

@cindoum Thank you very much, that solved my issue.

LuisMCunha avatar Aug 31 '16 13:08 LuisMCunha