Calling ref.on() or ref.once() is not working
In my constructor, I have injected @Inject(FirebaseRef) private ref:Firebase
this.ref.on("value", function(snapshot) { // does not execute below code console.log(snapshot); });
Cordova CLI: 6.0.0 Gulp version: CLI version 3.9.1 Gulp local: Local version 3.9.1 Ionic Version: 2.0.0-beta.3 Ionic CLI Version: 2.0.0-beta.22 Ionic App Lib Version: 2.0.0-beta.12 ios-deploy version: Not installed ios-sim version: 5.0.6 OS: Mac OS X El Capitan Node Version: v5.9.0 Xcode version: Xcode 7.3 Build version 7D175
"dependencies": {
"angular2": "2.0.0-beta.7",
"angularfire2": "^2.0.0-alpha.10",
"firebase": "^2.4.1",
"es6-promise": "3.0.2",
"es6-shim": "0.33.13",
"ionic-angular": "2.0.0-beta.3",
"ionic-native": "^1.0.12",
"ionicons": "3.0.0-alpha.3",
"reflect-metadata": "0.1.2",
"rxjs": "5.0.0-beta.2",
"zone.js": "0.5.15"
},
I'm assuming this.ref is an instance of AngularFire, like below:
export class MyApp {
constructor(ref: AngularFire) {
this.ref = ref; // <- ref is of type AngularFire
}
}
The AngularFire class does not provide direct access to the underlying Firebase reference. I think what you're looking for is FirebaseRef, which provides direct access to Firebase methods like .on() and .once()
@TylerEich Sorry. I miss the declaration. I have injected the FirebaseRef in my constructor with @Inject(FirebaseRef) private ref:Firebase And when calling the .on() or .once(), it doesn't execute.
Can you share more code? Maybe a Plunk that demonstrates the problem?
https://plnkr.co/edit/PQc396efOsH8eDqJZjKv?p=info
I couldn't add angularfire2 in plunker for some reason.
Your Plunk's main.ts file doesn't give any providers to the bootstrap() function. You need something like FIREBASE_PROVIDERS and defaultFirebase('https://<your-firebase>.firebaseio.com'). Is that something you left out of the Plunk on purpose?
https://github.com/angular/angularfire2#7-bootstrap
So sorry. I now figured out my problem My bad =(. It was in the Firebase security I didn't set the rules for the child node. I forgot that by default, child nodes are not accessible until you set the security rules for that node. Thanks for your help @TylerEich
No worries! Feedback helps a lot during alpha, even if it only makes the docs and errors better 😎👍