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 .

11
  • Shouldn't you return the value? Like return firebase.database().... Commented Dec 9, 2017 at 19:54
  • u mean like this : fbGetData() { return firebase.database().ref('/').on('child_added', (snapshot) => { return snapshot.val(); });} Commented Dec 9, 2017 at 19:59
  • I mean that getWorkers does not return anything in your original code. Commented Dec 9, 2017 at 19:59
  • yes .. then how to make it return something ? Commented Dec 9, 2017 at 20:00
  • Can you put return in front of firebase (like you said in your comment above)? Commented Dec 9, 2017 at 20:00

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

: Error : () => Observable<void>' is not assignable to type 'Observable<any> , so i tried Observable.bind(fn) , and then found the error : "subscribe is not a function "
@HimanshuKhandelwal, try: return Observable.bindCallback(fn) as Observable<any>
2

your first method must return observable.

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

    });
}

4 Comments

Observable<any> is still red , "A function whose declared type is neither 'void' nor 'any' must return a value."
This is wrong, because the "return" statement will return the inner function (the one that starts with (snapshot) =>{})
What is the variable firebase?
@Blunderchips import * as firebase from 'firebase';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.