Angular doesn't return the expected value









up vote
0
down vote

favorite












I'm a bit new to Angular (7). I'm trying to retrieve the status code when I do an HTTP request. Here's the code I use in a service :



checkIfSymbolExists() 
return this.http.get(this.url, observe: 'response' )
.subscribe(response =>
return response.status;
);



And I use the returned value in a method in one of my components like this :



onSubmit() 
console.log(this.stocks.checkIfSymbolExists());



I was expecting a number to be returned, but instead I have an object :



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


When, instead of simply returning response.status I do a console.log of it, I do get the 200 status code as expected (a number, and not an object).



Any ideas why it's not the same behavior when returning the value of response.status as shown here ? Thanks.










share|improve this question























  • What is your backend API code?
    – Aparna
    Nov 10 at 17:44














up vote
0
down vote

favorite












I'm a bit new to Angular (7). I'm trying to retrieve the status code when I do an HTTP request. Here's the code I use in a service :



checkIfSymbolExists() 
return this.http.get(this.url, observe: 'response' )
.subscribe(response =>
return response.status;
);



And I use the returned value in a method in one of my components like this :



onSubmit() 
console.log(this.stocks.checkIfSymbolExists());



I was expecting a number to be returned, but instead I have an object :



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


When, instead of simply returning response.status I do a console.log of it, I do get the 200 status code as expected (a number, and not an object).



Any ideas why it's not the same behavior when returning the value of response.status as shown here ? Thanks.










share|improve this question























  • What is your backend API code?
    – Aparna
    Nov 10 at 17:44












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm a bit new to Angular (7). I'm trying to retrieve the status code when I do an HTTP request. Here's the code I use in a service :



checkIfSymbolExists() 
return this.http.get(this.url, observe: 'response' )
.subscribe(response =>
return response.status;
);



And I use the returned value in a method in one of my components like this :



onSubmit() 
console.log(this.stocks.checkIfSymbolExists());



I was expecting a number to be returned, but instead I have an object :



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


When, instead of simply returning response.status I do a console.log of it, I do get the 200 status code as expected (a number, and not an object).



Any ideas why it's not the same behavior when returning the value of response.status as shown here ? Thanks.










share|improve this question















I'm a bit new to Angular (7). I'm trying to retrieve the status code when I do an HTTP request. Here's the code I use in a service :



checkIfSymbolExists() 
return this.http.get(this.url, observe: 'response' )
.subscribe(response =>
return response.status;
);



And I use the returned value in a method in one of my components like this :



onSubmit() 
console.log(this.stocks.checkIfSymbolExists());



I was expecting a number to be returned, but instead I have an object :



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


When, instead of simply returning response.status I do a console.log of it, I do get the 200 status code as expected (a number, and not an object).



Any ideas why it's not the same behavior when returning the value of response.status as shown here ? Thanks.







javascript angular typescript http






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 18:02









NinjaJami

3,14082952




3,14082952










asked Nov 10 at 17:41









tomfl

10715




10715











  • What is your backend API code?
    – Aparna
    Nov 10 at 17:44
















  • What is your backend API code?
    – Aparna
    Nov 10 at 17:44















What is your backend API code?
– Aparna
Nov 10 at 17:44




What is your backend API code?
– Aparna
Nov 10 at 17:44












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










You're doing it the wrong way. Here's the correct way of doing this:



First you return a mapped response from http.get instead of subscribeing from there. So you'll need to use .pipe(map(...)) instead of subscribe:



import map from 'rxjs/operators';
...
checkIfSymbolExists()
return this.http.get(this.url, observe: 'response' )
.pipe(
map(res => (res.status === 200))
);



And then you return the observable from checkIfSymbolExists and then subscribe to it in the onSubmit method:



onSubmit() 
this.stocks.checkIfSymbolExists()
.subscribe(res => console.log(res));
// This should print true if status is 200. false instead.



Explaination:



The responsibility of your service method checkIfSymbolExists() is to give the Component what it wants. So basically your Component doesn't need to know where exactly is your service getting this data from. It just needs to get a boolean on subscribing to the Observable returned by checkIfSymbolExists()



Now the checkIfSymbolExists() method gets response and you've also added an option to observe the complete response. map is just an Rxjs operator that will transform the response. Inside map what we're doing is checking for res.status which we will get because we have observed the response by doing observe: 'response'



Now the map will return whatever is returned by the comparison operator === which will return true if status is 200 and false otherwise.



Hope this gives you a better understanding.






share|improve this answer






















  • Thanks for the answer. What if I wanna check the status code inside of the service's method ? Like this : response => if (response.status === 200) return true; else return false; (edit : sorry for the indentation)
    – tomfl
    Nov 10 at 17:51











  • @tomfl, the updated answer should do that,
    – SiddAjmera
    Nov 10 at 17:52










  • You are very fast XD. And it worked ! Thank you so much ! I'm not exctly sure what all those methods do, but I can see that I still have a lot to learn ^^
    – tomfl
    Nov 10 at 17:57










  • @tomfl, I've added the explanation. Hope that helps you understand a bit of what's happening.
    – SiddAjmera
    Nov 10 at 18:03






  • 1




    Yes. You can say that. Whatever you write inside the subscribe block will get called only once the response is received and then mapped by the service.
    – SiddAjmera
    Nov 10 at 18:12










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%2f53241693%2fangular-doesnt-return-the-expected-value%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










You're doing it the wrong way. Here's the correct way of doing this:



First you return a mapped response from http.get instead of subscribeing from there. So you'll need to use .pipe(map(...)) instead of subscribe:



import map from 'rxjs/operators';
...
checkIfSymbolExists()
return this.http.get(this.url, observe: 'response' )
.pipe(
map(res => (res.status === 200))
);



And then you return the observable from checkIfSymbolExists and then subscribe to it in the onSubmit method:



onSubmit() 
this.stocks.checkIfSymbolExists()
.subscribe(res => console.log(res));
// This should print true if status is 200. false instead.



Explaination:



The responsibility of your service method checkIfSymbolExists() is to give the Component what it wants. So basically your Component doesn't need to know where exactly is your service getting this data from. It just needs to get a boolean on subscribing to the Observable returned by checkIfSymbolExists()



Now the checkIfSymbolExists() method gets response and you've also added an option to observe the complete response. map is just an Rxjs operator that will transform the response. Inside map what we're doing is checking for res.status which we will get because we have observed the response by doing observe: 'response'



Now the map will return whatever is returned by the comparison operator === which will return true if status is 200 and false otherwise.



Hope this gives you a better understanding.






share|improve this answer






















  • Thanks for the answer. What if I wanna check the status code inside of the service's method ? Like this : response => if (response.status === 200) return true; else return false; (edit : sorry for the indentation)
    – tomfl
    Nov 10 at 17:51











  • @tomfl, the updated answer should do that,
    – SiddAjmera
    Nov 10 at 17:52










  • You are very fast XD. And it worked ! Thank you so much ! I'm not exctly sure what all those methods do, but I can see that I still have a lot to learn ^^
    – tomfl
    Nov 10 at 17:57










  • @tomfl, I've added the explanation. Hope that helps you understand a bit of what's happening.
    – SiddAjmera
    Nov 10 at 18:03






  • 1




    Yes. You can say that. Whatever you write inside the subscribe block will get called only once the response is received and then mapped by the service.
    – SiddAjmera
    Nov 10 at 18:12














up vote
1
down vote



accepted










You're doing it the wrong way. Here's the correct way of doing this:



First you return a mapped response from http.get instead of subscribeing from there. So you'll need to use .pipe(map(...)) instead of subscribe:



import map from 'rxjs/operators';
...
checkIfSymbolExists()
return this.http.get(this.url, observe: 'response' )
.pipe(
map(res => (res.status === 200))
);



And then you return the observable from checkIfSymbolExists and then subscribe to it in the onSubmit method:



onSubmit() 
this.stocks.checkIfSymbolExists()
.subscribe(res => console.log(res));
// This should print true if status is 200. false instead.



Explaination:



The responsibility of your service method checkIfSymbolExists() is to give the Component what it wants. So basically your Component doesn't need to know where exactly is your service getting this data from. It just needs to get a boolean on subscribing to the Observable returned by checkIfSymbolExists()



Now the checkIfSymbolExists() method gets response and you've also added an option to observe the complete response. map is just an Rxjs operator that will transform the response. Inside map what we're doing is checking for res.status which we will get because we have observed the response by doing observe: 'response'



Now the map will return whatever is returned by the comparison operator === which will return true if status is 200 and false otherwise.



Hope this gives you a better understanding.






share|improve this answer






















  • Thanks for the answer. What if I wanna check the status code inside of the service's method ? Like this : response => if (response.status === 200) return true; else return false; (edit : sorry for the indentation)
    – tomfl
    Nov 10 at 17:51











  • @tomfl, the updated answer should do that,
    – SiddAjmera
    Nov 10 at 17:52










  • You are very fast XD. And it worked ! Thank you so much ! I'm not exctly sure what all those methods do, but I can see that I still have a lot to learn ^^
    – tomfl
    Nov 10 at 17:57










  • @tomfl, I've added the explanation. Hope that helps you understand a bit of what's happening.
    – SiddAjmera
    Nov 10 at 18:03






  • 1




    Yes. You can say that. Whatever you write inside the subscribe block will get called only once the response is received and then mapped by the service.
    – SiddAjmera
    Nov 10 at 18:12












up vote
1
down vote



accepted







up vote
1
down vote



accepted






You're doing it the wrong way. Here's the correct way of doing this:



First you return a mapped response from http.get instead of subscribeing from there. So you'll need to use .pipe(map(...)) instead of subscribe:



import map from 'rxjs/operators';
...
checkIfSymbolExists()
return this.http.get(this.url, observe: 'response' )
.pipe(
map(res => (res.status === 200))
);



And then you return the observable from checkIfSymbolExists and then subscribe to it in the onSubmit method:



onSubmit() 
this.stocks.checkIfSymbolExists()
.subscribe(res => console.log(res));
// This should print true if status is 200. false instead.



Explaination:



The responsibility of your service method checkIfSymbolExists() is to give the Component what it wants. So basically your Component doesn't need to know where exactly is your service getting this data from. It just needs to get a boolean on subscribing to the Observable returned by checkIfSymbolExists()



Now the checkIfSymbolExists() method gets response and you've also added an option to observe the complete response. map is just an Rxjs operator that will transform the response. Inside map what we're doing is checking for res.status which we will get because we have observed the response by doing observe: 'response'



Now the map will return whatever is returned by the comparison operator === which will return true if status is 200 and false otherwise.



Hope this gives you a better understanding.






share|improve this answer














You're doing it the wrong way. Here's the correct way of doing this:



First you return a mapped response from http.get instead of subscribeing from there. So you'll need to use .pipe(map(...)) instead of subscribe:



import map from 'rxjs/operators';
...
checkIfSymbolExists()
return this.http.get(this.url, observe: 'response' )
.pipe(
map(res => (res.status === 200))
);



And then you return the observable from checkIfSymbolExists and then subscribe to it in the onSubmit method:



onSubmit() 
this.stocks.checkIfSymbolExists()
.subscribe(res => console.log(res));
// This should print true if status is 200. false instead.



Explaination:



The responsibility of your service method checkIfSymbolExists() is to give the Component what it wants. So basically your Component doesn't need to know where exactly is your service getting this data from. It just needs to get a boolean on subscribing to the Observable returned by checkIfSymbolExists()



Now the checkIfSymbolExists() method gets response and you've also added an option to observe the complete response. map is just an Rxjs operator that will transform the response. Inside map what we're doing is checking for res.status which we will get because we have observed the response by doing observe: 'response'



Now the map will return whatever is returned by the comparison operator === which will return true if status is 200 and false otherwise.



Hope this gives you a better understanding.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 at 18:02

























answered Nov 10 at 17:43









SiddAjmera

9,68321037




9,68321037











  • Thanks for the answer. What if I wanna check the status code inside of the service's method ? Like this : response => if (response.status === 200) return true; else return false; (edit : sorry for the indentation)
    – tomfl
    Nov 10 at 17:51











  • @tomfl, the updated answer should do that,
    – SiddAjmera
    Nov 10 at 17:52










  • You are very fast XD. And it worked ! Thank you so much ! I'm not exctly sure what all those methods do, but I can see that I still have a lot to learn ^^
    – tomfl
    Nov 10 at 17:57










  • @tomfl, I've added the explanation. Hope that helps you understand a bit of what's happening.
    – SiddAjmera
    Nov 10 at 18:03






  • 1




    Yes. You can say that. Whatever you write inside the subscribe block will get called only once the response is received and then mapped by the service.
    – SiddAjmera
    Nov 10 at 18:12
















  • Thanks for the answer. What if I wanna check the status code inside of the service's method ? Like this : response => if (response.status === 200) return true; else return false; (edit : sorry for the indentation)
    – tomfl
    Nov 10 at 17:51











  • @tomfl, the updated answer should do that,
    – SiddAjmera
    Nov 10 at 17:52










  • You are very fast XD. And it worked ! Thank you so much ! I'm not exctly sure what all those methods do, but I can see that I still have a lot to learn ^^
    – tomfl
    Nov 10 at 17:57










  • @tomfl, I've added the explanation. Hope that helps you understand a bit of what's happening.
    – SiddAjmera
    Nov 10 at 18:03






  • 1




    Yes. You can say that. Whatever you write inside the subscribe block will get called only once the response is received and then mapped by the service.
    – SiddAjmera
    Nov 10 at 18:12















Thanks for the answer. What if I wanna check the status code inside of the service's method ? Like this : response => if (response.status === 200) return true; else return false; (edit : sorry for the indentation)
– tomfl
Nov 10 at 17:51





Thanks for the answer. What if I wanna check the status code inside of the service's method ? Like this : response => if (response.status === 200) return true; else return false; (edit : sorry for the indentation)
– tomfl
Nov 10 at 17:51













@tomfl, the updated answer should do that,
– SiddAjmera
Nov 10 at 17:52




@tomfl, the updated answer should do that,
– SiddAjmera
Nov 10 at 17:52












You are very fast XD. And it worked ! Thank you so much ! I'm not exctly sure what all those methods do, but I can see that I still have a lot to learn ^^
– tomfl
Nov 10 at 17:57




You are very fast XD. And it worked ! Thank you so much ! I'm not exctly sure what all those methods do, but I can see that I still have a lot to learn ^^
– tomfl
Nov 10 at 17:57












@tomfl, I've added the explanation. Hope that helps you understand a bit of what's happening.
– SiddAjmera
Nov 10 at 18:03




@tomfl, I've added the explanation. Hope that helps you understand a bit of what's happening.
– SiddAjmera
Nov 10 at 18:03




1




1




Yes. You can say that. Whatever you write inside the subscribe block will get called only once the response is received and then mapped by the service.
– SiddAjmera
Nov 10 at 18:12




Yes. You can say that. Whatever you write inside the subscribe block will get called only once the response is received and then mapped by the service.
– SiddAjmera
Nov 10 at 18:12

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241693%2fangular-doesnt-return-the-expected-value%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

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党