I am trying to create a typescript object from an HTTP response in angular8, But I receive an error:
ERROR ReferenceError: Profile is not defined
at SafeSubscriber._next (app.service.ts:17)
at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUnsub (Subscriber.js:196)
at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.next
(Subscriber.js:134)
at Subscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._next
(Subscriber.js:77)
at Subscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next
(Subscriber.js:54)
at MapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/map.js.MapSubscriber._next
(map.js:41)
My Profile.ts file is : here
My app.service.ts file is: here
I construct a component class object with this service result and I also get the error
ERROR TypeError: Cannot read property ‘name’ of undefined
when I use it in the HTML file.
The console output for the statement in service file is:
{
name: "somename",
status: "sometext",
bio: "sometext",
email: "email",
gitlab: "link",
twitter: "link",
instagram: "link",
linkedin: "link",
telegram: "link"
},
This is output accompanied by the errors.
My HTML code shortly is :
<p class="ui header">{{profile.name}}</p>
My component class file is: here
2
Answers
The issue is in
getProfile()
method of yourAppService
. You are returningthis.profile
from this method, instead, you should return an observable like this:Now in component inject this service in the component constructor like this:
Use a safe operator in the template –
You don’t need extra code to map to
Profile
object. HttpClient would do it for you.You need to make few changes to service.ts as below:
In Component:
And in you html: