Firebase Subscribe to Database not working


#1

I should start out by saying I am new to Ionic and I am trying to learn Ionic through the various tutorials online. I have run into an issue and cannot get past it. I am setting up a simple chat application and can push messages to firebase through…

this.db.list('/chat').push({
  		username: this.username,
  		message: this.message
  	})

When I attempt to pull data back through…

this.db.object('/chat/').subscribe( data => {
        console.log(data);
      })

I get the following error…

Runtime Error
Uncaught (in promise): TypeError: this.db.object(…).subscribe is not a function TypeError: this.db.object(…).subscribe is not a function at new ChatPage

I have the following in the import section of the .ts file…

import { AngularFireDatabase, FirebaseListObservable } from ‘angularfire2/database’;

I’ve been trying a bunch of different methods, but nothing seems to work. Any help would be appreciated.

Thanks,
Ben


#2

try

this.db.list('/chat/').subscribe( data => {
        console.log(data);
      })

Note angularfire2 has recently changed https://github.com/angular/angularfire2/blob/master/docs/version-5-upgrade.md

hth
Tim


#3

What version of AngularFire2 are you using? In version 5RC they changed the database API, FirebaseListObservable and FirebaseObjectObservable were removed in favor of AngularFireList and AngularFireObject.

Go through their docs, there were a few breaking changes (all of them in the Database API).


#4

Tim,

Thanks for the suggestion. It still failed…

Runtime Error
Uncaught (in promise): TypeError: this.db.list(…).subscribe is not a function TypeError: this.db.list(…).subscribe is not a function at new ChatPage


#5

Jave,

I am thanks for the reply, I am running…

"angularfire2": "^5.0.0-rc.2",

I’ll go through the docs and reply with an update.

Thanks,
Ben


#6

You could always use a

firebase.database().ref('chat')

Could you not? The two are essentially synonymous if I understand correctly. But I may not.


#7

@tja2353 you posted the outdated subscribe logic

This is the new syntax

constructor(afDb: AngularFireDatabase) {
  afDb.list<T>('items').valueChanges().subscribe(console.log);
}

#8

Thanks for the new logic @LoLStats , that did the trick!!

Ben


#9

its showing errors.

Object(...) is not a function

my applied code was
this.database.list<Review>('reviews').valueChanges().subscribe(console.log);