I am trying to make an HTTP get request at takes a userId as a parameter and returns a single user profile using Angular, but keep getting undefined data returned. I know the problem isn't with my backend server because the same HTTP get request with Postman works correctly. Also, I am getting this exception (backend is written in Java) from my Angular HTTP request, but not from the Postman HTTP request.
profile.component.ts:
profile: Profile;
constructor(private profileService: ProfileService) {
}
ngOnInit() {
this.getProfile();
}
getProfile() {
this.profileService.getProfile("5e7bd87e05854a05cc0f6898").subscribe(
profile => this.profile = profile,
);
console.log( this.profile );
}
profile.service.ts:
getProfile(userId: string) {
let params = new HttpParams().set("id", userId);
console.log( "executing HTTP get" );
//return this.httpClient.get<any>( "http://localhost:8080/user", { params: params });
// I've tried this above method and the one below
return this.httpClient.get("http://localhost:8080/user", { params: params })
.pipe(
map((data: any) => {
const profile: Profile = new Profile( data.object.id,
data.object.username,
data.object.password,
data.object.fname,
data.object.lname,
data.object.email,
data.object.joined );
return profile;
})
);
}
The console.log( this.profile )
just comes out as undefined
in the browser console. I think I'm using subscribe
incorrectly. Anyone know what I'm doing wrong?
EDIT: Here is a screenshot of the error from the browser console. Not sure if it's relevant.