I'm using AngularFireAuth module to get the current user's UID to the component on page load through route resolving, but the lines of code needed to return the unwrapped promise to the component don't seem to be resolving and leave the promise unfulfilled.
relevant Routing Module Code for the edit-profiles module:
const routes: Routes = [
{
path: '',
component: EditProfilesComponent,
resolve: { userUID: UserInfoService },
},
];
The resolver service code is as follows:
export class UserInfoService implements Resolve<any> {
constructor(public afAuth: AngularFireAuth, public af: AngularFirestore) {}
async resolve() {
const user = await this.afAuth.currentUser;
const userUID = user?.uid;
return userUID; //returns undefined
}
}
I've also tried making it one promise and resolving it but that returned undefined as well. The code looked like this:
const promise = new Promise(async(resolve, reject) => {
const user = await this.afAuth.currentUser
const userUID = user?.uid;
resolve(userUID)
})
And then the EditProfiles consumer component code is:
export class UserInfoService implements Resolve<any> {
constructor(public afAuth: AngularFireAuth, public af: AngularFirestore) {}
async resolve() {
const user = await this.afAuth.currentUser;
const userUID = user?.uid;
return userUID; //returns undefined
}
}
I'm really not sure how to make it so that the promise returns the userUID to be used and consumed by that component, and display user-specific data, but I'm not sure how to make sure it's there before the component is instantiated.
@Bravo was right it was a problem with the design of the code it turns out what I actually wanted was for the page to wait until the promise was resolved to print out the data so what was needed on the ngOnInit was the following code
ngOnInit(): void {
this.afAuth.authState.subscribe((user) => {
if(user) {
//write logic here to make sure the promise gets resolved
} else {
//logic for when the page is getting the information
}
})
}
This is how you make sure that promises sent from firebase are resolved, you subscribe to the authstate observable and when that observable returns true, then you can finally retrieve the information from firebase that you need, that's the key piece that I was missing that it turns out that route resolvers may not have been the best tool for.