4

I am trying to subscribe to an function returning observable object in angular 2 . So when there is any new worker added the main calling function should have the value

// functiion based on firebase DB

getWorkers():Observable<any> {
    firebase.database().ref('/workers').on('child_added', (snapshot) => {
        return snapshot.val();

    });

}

subscriber function

workers: any[];
public ngOnInit() {
    this.db.getWorkers().subscribe(workers => this.workers = workers);
}

It is saying the function return type is not Observable and hence throwing error .

R. Richards
  • 24,603
  • 10
  • 64
  • 64
Mohit H
  • 927
  • 3
  • 11
  • 26
  • Shouldn't you return the value? Like `return firebase.database()...`. – ConnorsFan Dec 09 '17 at 19:54
  • u mean like this : fbGetData() { return firebase.database().ref('/').on('child_added', (snapshot) => { return snapshot.val(); });} – Mohit H Dec 09 '17 at 19:59
  • I mean that `getWorkers` does not return anything in your original code. – ConnorsFan Dec 09 '17 at 19:59
  • yes .. then how to make it return something ? – Mohit H Dec 09 '17 at 20:00
  • Can you put `return` in front of `firebase` (like you said in your comment above)? – ConnorsFan Dec 09 '17 at 20:00
  • Error: Uncaught (in promise): Error: StaticInjectorError[DatabaseService]: StaticInjectorError[DatabaseService]: NullInjectorError: No provider for DatabaseService! Error: StaticInjectorError[DatabaseService] – Mohit H Dec 09 '17 at 20:03
  • Check [this answer](https://stackoverflow.com/a/47380256/1009922) for that `NullInjectorError`. – ConnorsFan Dec 09 '17 at 20:06
  • @connorsFan you cannot return firebase.database... since it does not return an observable. It could work with promises though... – rotemx Dec 09 '17 at 22:47
  • @rotemx - I don't know if `firebase().database().ref(...).on(...)` returns an Observable or not (I don't know anything about firebase). I just see that `getWorkers` has a return type but does not return anything. – ConnorsFan Dec 09 '17 at 22:52
  • @ConnorsFan yes but returning some unknown type would not help, as the return value of `getWorkers()` should be "Observable" – rotemx Dec 09 '17 at 22:54
  • Did you find the solution? If yes then please tell me. I have got the same issue. – Robert Williams Feb 05 '18 at 18:48

2 Answers2

3
  • firebase.database() does not return an Observable so you cannot return it.

  • if you return Observable.of(val) from within the callback function as suggested below, it will only return the callback function and not the outer getWorkers() function.

You need to create an observable from the data returned from the callback.

I would use Observable.bindCallback:

getWorkers():Observable<any> {    
  let fn = firebase.database().ref('/workers').on('child_added', (snapshot) => {
        return snapshot.val();
  });

  return Observable.bindCallback(fn) as Observable<any>
}

then you can use it as any observable:

this.getWorkers().subscribe(data=>{
    ...[code]...
})

here is some info from the docs

rotemx
  • 944
  • 7
  • 17
  • : Error : () => Observable' is not assignable to type 'Observable , so i tried Observable.bind(fn) , and then found the error : "subscribe is not a function " – Mohit H Dec 11 '17 at 11:25
  • @HimanshuKhandelwal, try: ```return Observable.bindCallback(fn) as Observable``` – rotemx Dec 13 '17 at 09:42
2

your first method must return observable.

getWorkers():Observable<any> {
    firebase.database().ref('/workers').on('child_added', (snapshot) => {
        return Observable.of(snapshot.val());

    });
}
hamid_reza hobab
  • 925
  • 9
  • 21