Angularfire: No documentation on using FirebaseRef

Created on 22 Jul 2016  路  8Comments  路  Source: angular/angularfire

I would like to use some of the API functionality other than the object and list observable within my application, but there is no documentation. I'm learning Angular 2 at the same time, so I'm struggling with a framework and the API. I'm trying to use FirebaseRef so that I have access to some of the API calls like once(), off(), on(). Is there no way to do this currently? I'm grasping here. I've moved code all over the place and can't get the once() call to work.

Is this functionality supported yet? If it's not, I'll be forced to write my application in Angular1/AngularFire because it is something I know already.

Help Wanted! docs

Most helpful comment

Angularfire2 is more of an abstraction on top of the firebase library. It was made so one could use firebase and angular2 coming from a non-firebase background and still enjoy all the cool features it has to offer. With that in mind, it is a fact that Angularfire does not have any API for using the traditional firebase reference and the on(), off(), once() methods to listen for changes in your data. Instead, it uses RxJS Observables to do all that. RxJS is becoming a lot more popular now, so for those who are familiarized with it, AngularFire2 is really easy to use and to wrap your head around.

With Angularfire2, you should not need to write any .on() or .off(). You can accomplish the same thing by pointing to a reference and then subscribing to it, like so:

let subscription = this.af.database.object('someLocation').subscribe(data=> {
//do something with your data
})

Which is essentially the same as:

firebase.database().ref('someLocation').on('value', snapshot=>{
//do something with your data
})

and then, when you are done, instead of using the off() method, you can call unsubscribe()

subscription.unsubscribe();

And finally, the equivalent of .once() would probably be something like:

this.af.database.object('someLocation').take(1).subscribe(...) 

Firebase actually encourages you to look at your data as either an object or a list, so you should have all the use cases covered when using AngularFire2. However, if you feel like you can't do something with angularfire but can with firebase, then just use the firebase library for that. I know it's not optimal, but it's an option until you learn how to accomplish the same thing with angularfire2.

All 8 comments

Angularfire2 is more of an abstraction on top of the firebase library. It was made so one could use firebase and angular2 coming from a non-firebase background and still enjoy all the cool features it has to offer. With that in mind, it is a fact that Angularfire does not have any API for using the traditional firebase reference and the on(), off(), once() methods to listen for changes in your data. Instead, it uses RxJS Observables to do all that. RxJS is becoming a lot more popular now, so for those who are familiarized with it, AngularFire2 is really easy to use and to wrap your head around.

With Angularfire2, you should not need to write any .on() or .off(). You can accomplish the same thing by pointing to a reference and then subscribing to it, like so:

let subscription = this.af.database.object('someLocation').subscribe(data=> {
//do something with your data
})

Which is essentially the same as:

firebase.database().ref('someLocation').on('value', snapshot=>{
//do something with your data
})

and then, when you are done, instead of using the off() method, you can call unsubscribe()

subscription.unsubscribe();

And finally, the equivalent of .once() would probably be something like:

this.af.database.object('someLocation').take(1).subscribe(...) 

Firebase actually encourages you to look at your data as either an object or a list, so you should have all the use cases covered when using AngularFire2. However, if you feel like you can't do something with angularfire but can with firebase, then just use the firebase library for that. I know it's not optimal, but it's an option until you learn how to accomplish the same thing with angularfire2.

@lf-alves Thank you so much for the awesome response. It was well thought out and helpful. My biggest concern was using once(), but the solution you proposed already has been proved out in my app!

It's good to know that I will be able to accomplish everything I need to in angularfire2. It's just the task of figuring out how to do it that will be my biggest hurdle.

Hey I get the issue Property 'take' does not exist on type 'FirebaseObjectObservable<any>' when I add .take() to my code as provided in the example above. Any help here?

@sean-hill take is a RxJS filtering operator
Try:

import 'rxjs/add/operator/take'

See #456 for more info and examples

FirebaseRef is no longer in the API. Use the FirebaseApp token for injecting the initialized app instance:

import { FirebaseApp } from 'angularfire2/app';

constructor(app: FirebaseApp) { }

this works for me
constructor(public db: AngularFireDatabase
)
...
...
function someThing(){
return this.db.list("/dataBase").query.once("value")
}

regards

Hi,
try take with pipe:

     import { take } from 'rxjs/operators';
     this.af.database.object('someLocation').pipe(take(1)).subscribe(...) 

@lf-alves thank you a lot. you are a lifesaver

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Leanvitale picture Leanvitale  路  3Comments

itisparas picture itisparas  路  3Comments

jteplitz picture jteplitz  路  3Comments

Sun3 picture Sun3  路  3Comments

Maistho picture Maistho  路  3Comments