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
) { }
}









share|improve this question
























  • 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















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
) { }
}









share|improve this question
























  • 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













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
) { }
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Bressuire

Vorschmack

Quarantine