How can I resolve so that resolve returns the Object itself instead of Observable with Angular?
up vote
0
down vote
favorite
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
) { }
}
angular
|
show 1 more comment
up vote
0
down vote
favorite
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
) { }
}
angular
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 at 21:08
I just posted the User class, it has a cellphone property.
– Alex
Nov 11 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 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 at 21:17
1
What do you see in the console?
– JB Nizet
Nov 11 at 21:19
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
) { }
}
angular
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
) { }
}
angular
angular
edited Nov 11 at 21:13
asked Nov 11 at 21:03
Alex
1,53331839
1,53331839
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 at 21:08
I just posted the User class, it has a cellphone property.
– Alex
Nov 11 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 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 at 21:17
1
What do you see in the console?
– JB Nizet
Nov 11 at 21:19
|
show 1 more comment
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 at 21:08
I just posted the User class, it has a cellphone property.
– Alex
Nov 11 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 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 at 21:17
1
What do you see in the console?
– JB Nizet
Nov 11 at 21:19
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 at 21:08
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 at 21:08
I just posted the User class, it has a cellphone property.
– Alex
Nov 11 at 21:14
I just posted the User class, it has a cellphone property.
– Alex
Nov 11 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 at 21:16
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 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 at 21:17
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 at 21:17
1
1
What do you see in the console?
– JB Nizet
Nov 11 at 21:19
What do you see in the console?
– JB Nizet
Nov 11 at 21:19
|
show 1 more comment
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253220%2fhow-can-i-resolve-so-that-resolve-returns-the-object-itself-instead-of-observabl%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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 at 21:08
I just posted the User class, it has a cellphone property.
– Alex
Nov 11 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 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 at 21:17
1
What do you see in the console?
– JB Nizet
Nov 11 at 21:19