I want to make a method on a service that works with AngularFire2 and returns an Observable. I want to update a counter (updating a counter may not be a very scalable example, but that does not really matter here). This is my attempt for the service:
import { Injectable } from '@angular/core';
import { AngularFire } from 'angularfire2';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
@Injectable()
export class MyService {
constructor(private af: AngularFire) { }
updateCounter(): Observable<any> {
let counterObserver = this.af.database.object('/variousdata/counter');
counterObserver.take(1).subscribe(res => {
let counter = res.$value;
return this.af.database.object('/variousdata').update({counter: counter+1});
});
return counterObserver;
}
}
And I am using it like this:
let resultObservable = this.myService.updateCounter();
resultObservable.subscribe(
(obj) => {
console.log('result:', JSON.stringify(obj));
}
}
But I am getting only the first result (getting the counter value) printed to the console. How can I get also (or just only) the second one?
I have been somewhat inspired by the example found in Observable type with two http.get calls in Angular 2, but this example did not work so good for my Firebase case, so I have tried to alter it...