Working with Observables has been an interesting experiment for me, but I'm facing an issue that I can't seem to resolve. While all the methods work perfectly fine when called outside the pipe, the problem arises when I nest them like this:
createUserWithEmailAndPassword(email, password): Observable<Session> {
return from(this.firebaseAuth.createUserWithEmailAndPassword(email, password))
.pipe(map(v => v.user as unknown as User),
switchMap(this.userDatabase.createUser),
map(this.userDatabase.createSession)
);
}
This results in the following error being thrown:
ERROR TypeError: Cannot read property 'post' of undefined
at SwitchMapSubscriber.create (database-core.service.ts:57) at SwitchMapSubscriber.createUser [as project] (database-user.service.ts:28) at SwitchMapSubscriber._next (switchMap.js:30) at SwitchMapSubscriber.next (Subscriber.js:49) at MapSubscriber._next (map.js:35) at MapSubscriber.next (Subscriber.js:49) at subscribeToPromise.js:5 at ZoneDelegate.invoke (zone-evergreen.js:364) at Object.onInvoke (core.js:27437) at ZoneDelegate.invoke (zone-evergreen.js:363)
database-user:
createUser(user: User): Observable<User> {
return super.create(this.DB_URL, user) //<--- line 28
.pipe(map(v => v as unknown as User));
}
createSession(user: User): Session {
const newSession: Session = {
firstLogin: false,
id: user.id
};
this.angularFirestore.collection('session').doc(user.uid).set(newSession)
.catch(e => {
throw new Error(e);
});
return newSession;
}
In database-core:
constructor(
private globalService: GlobalService,
protected httpClient: HttpClient
) {}
// (...)
protected create(url: string, data: any): Observable<JsonObject> {
return this.httpClient.post<JsonObject>(url, data, this.globalService.getHttpOptions()) //<-- lines 57
.pipe(catchError(DatabaseCoreService.handleError));
}
Subscribing to the observable:
createUser(email, password) {
this.authService.createUserWithEmailAndPassword(email, password).subscribe();
}
App-Module:
@NgModule({
declarations: [AppComponent],
entryComponents: [],
imports: [
BrowserModule,
IonicModule.forRoot(),
AppRoutingModule,
AngularFireModule.initializeApp(environment.firebaseConfig),
HttpClientModule
],
// (...)