I'm new to Angular (7) and I'm encountering an issue while trying to retrieve the status code from an HTTP request. Here's the code snippet used in a service :
checkIfSymbolExists() {
return this.http.get(this.url, { observe: 'response' })
.subscribe(response => {
return response.status;
});
}
The returned value is then utilized in a method within one of my components in the following manner :
onSubmit() {
console.log(this.stocks.checkIfSymbolExists());
}
Unexpectedly, instead of receiving a numerical value as desired, I am getting an object with the following structure:
Subscriber {closed: false, _parent: null, _parents: null, _subscriptions: Array(1), syncErrorValue: null, …}
closed: true
destination: SafeSubscriber {closed: true, _parent: null, _parents: null, _subscriptions: null, syncErrorValue: null, …}
isStopped: true
syncErrorThrowable: true
syncErrorThrown: false
syncErrorValue: null
_parent: null
_parentSubscription: null
_parents: null
_subscriptions: null
__proto__: Subscription
Interestingly, when I utilize console.log instead of simply returning response.status
, I do see the expected 200 status code displayed (a number, not an object).
Any insights on why the behavior changes when returning the value of response.status
? Thank you.