Hi I have a simple controller that returns a simple (I’ve simplified it all for this post):
[HttpGet("[action]")]
public AppSettings GetStuff()
{
var stuff = new Stuff { Dummy = ”Hi”; };
return stuff;
}
public class Stuff
{
public string Dummy { get; set; }
}
All good, now I call it from my Angular service:
getStuff(): Observable<IStuff> {
return (this.httpClient
.get<IStuff>(this.localUrl)
.pipe(
catchError(this.errorHandlerSerevice.handleError)
)) as any;
}
Here’s my IStuff:
export interface IStuff {
Dummy?: string;
}
All good… The console shows me 200 and the response…
But when I subscribe to getStuff() from my component I get [object object] or udentified… I have tried a god few subscriptions but no luck:
stuff: IStuff;
getStuff(){
this.stuffService.getStuff()
.subscribe(stuff => this.stuff = stuff);
}
Or:
getStuff(){
this.stuffService.getStuff()
.subscribe((data: IStuff) => this.stuff = {
Dummy: data['Dummy']
});
}
Any idea?
When you do this:
instance .subscribe(data => { this.stuff = data; alert(data); });
You're trying to print data
on an alert box. But since data
is a JavaScript Object, you're seeing [Object object]
as the alert will call toString
on it. If you want the actual contents to be printed, you'll have to stringify
it.
Try this:
alert(JSON.stringify(data));
This should print the actual response.