I am attempting to preload a user's profile before the profile page loads using resolver. But I am getting the error
Type 'Promise' is not assignable to type 'Observable'
This what my code currently looks like
profile.service.ts
import { AngularFirestore } from '@angular/fire/firestore';
import { Injectable } from '@angular/core';
import { AuthenticateService } from './../auth/authenticate.service';
@Injectable({
providedIn: 'root'
})
export class ProfileService {
constructor(public authService: AuthenticateService, private afs: AngularFirestore) { }
findUserProfile(id: string) {
console.log('User ID to search for is: ' + id);
return this.afs.collection('profiles').doc(id).ref.get().then(doc => {
return doc.data();
});
}
}
profile-resolver.service.ts
import { Injectable } from '@angular/core';
import { Router, Resolve, RouterStateSnapshot, ActivatedRouteSnapshot } from '@angular/router';
import { ProfileService } from './profile.service';
import { AuthenticateService } from 'src/app/auth/authenticate.service';
import { Observable } from 'rxjs';
import { Profile } from './profile';
@Injectable({
providedIn: 'root'
})
export class ProfileResolverService {
profile: Profile;
constructor(private ps: ProfileService, private router: Router, private authService: AuthenticateService) { }
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Profile> {
if (this.authService.user != null) {
const userId = this.authService.user.uid;
return this.ps.findUserProfile(userId);
}
}
}
I am using the resolver in profile.module.ts to advertise the resolver to the route like this
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { Routes, RouterModule } from '@angular/router';
import { IonicModule } from '@ionic/angular';
import { ProfilePage } from './profile.page';
import { ProfileResolverService } from './../profile-resolver.service';
const routes: Routes = [
{
path: '',
component: ProfilePage,
resolve: {
userProfile: ProfileResolverService
}
}
];
@NgModule({
imports: [
CommonModule,
FormsModule,
IonicModule,
RouterModule.forChild(routes)
],
declarations: [ProfilePage],
providers: [ProfileResolverService]
})
export class ProfilePageModule { }
Unfortunately my code is throwing up the Type Promise is not assignable to type 'Observable'
Why am I doing wrong and how do I fix it.