0

I have a resolver that looks like this:

import { Injectable } from '@angular/core';
import { Resolve, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { HttpClient } from '@angular/common/http';

import { Observable } from 'rxjs';

import { User } from './user';

@Injectable()
export class UserResolver implements Resolve<User> {

    constructor(private httpClient: HttpClient) {}

    resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<User> {
        const userId = +route.params['userId'];
        return this.httpClient.get<User>('/api/user/single/userId/' + userId);
    }
}

I'm using it like this in a RouterModule:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { SmsComponent } from './sms.component';
import { SmsSentComponent } from './sms-sent/sms-sent.component';
import { SmsNewComponent } from './sms-new/sms-new.component';
import { SmsPlannedComponent } from './sms-planned/sms-planned.component';

import { UserResolver } from '../users/user.resolver';

const routes: Routes = [
    {
        path: '',
        component: SmsComponent,
        children: [
            { path: 'sent', component: SmsSentComponent },
            // https://stackoverflow.com/questions/34208745/angular-2-optional-route-parameter
            { path: 'new/:userId', component: SmsNewComponent, resolve: { user: UserResolver } },
            { path: 'new', component: SmsNewComponent },
            { path: '', component: SmsPlannedComponent }
        ]
    }
];

@NgModule({
    imports: [RouterModule.forChild(routes)],
    exports: [RouterModule],
    providers: [
        UserResolver
    ]
})
export class SmsRoutingModule { }

And then I fetch the user inside ngOnInit() inside a component like this:

this.activatedRoute.data
    .subscribe((user: User) => {
        console.log(user);
    });

When I console.log the user it seems to work, but if I console.log this:

this.activatedRoute.data
    .subscribe((user: User) => {
        console.log(user.cellphone);
    });

Then I get undefined. It seems like the User is not ready when I run the console.log, is it not the whole point of resolving to have it ready when component loads? How can I have the User Object ready inside ngOnInit() as expected?

This is the User class by the way:

export class User {
    constructor(
        public id: number = 0,
        public email: string = '',
        public firstname: string = '',
        public lastName: string = '',
        public cellphone: string = '',
        public companyName: string = '',
        public address: string = '',
        public hasHeating: boolean = false,
        public heatingLastService: string = '',
        public createdTime: number = 0,
        public isCreatedByAdmin: boolean = false,
        public isDeleted: boolean = false
    ) { }
}
Alex
  • 5,671
  • 9
  • 41
  • 81
  • If you get undefined, it simply means there is no cellphone property in the user. It has nothing to do with the User not being ready. – JB Nizet Nov 11 '18 at 21:08
  • I just posted the User class, it has a cellphone property. – Alex Nov 11 '18 at 21:14
  • The class is irrelevant. What you get is an object deserialized from the JSON sent by the server. It's not an instance of that class. There is no cellphone property in the JSON object sent by the server. HttpClient doens't know anything about your class. – JB Nizet Nov 11 '18 at 21:16
  • If I console.log(user), the user itself, then it works. What does that mean? It seems like the properties are not ready when I console.log. If I just console.log the user itself, then I can see the values on the console. – Alex Nov 11 '18 at 21:17
  • 1
    What do you see in the console? – JB Nizet Nov 11 '18 at 21:19
  • Actually you are right, I get an object with the user object in it. Not the user object itself. – Alex Nov 11 '18 at 21:25

0 Answers0