How to fetch a boolean value from the result in angular http client (angular 5)?












0















I have Angular 5 app with the following service call:



let isExist: boolean;
this.http.get<Boolean>(`${this.baseUrl}/Trips/TripExist`, {
headers: new HttpHeaders({ 'Accept': 'text/plain', 'Content-Type': 'text/plain' }),
params: {id: id, name: name},
observe: 'response'
}).subscribe(
data => { isExist = data.body;
console.log(data);
},
err => console. error(err)
);
if (isExist == true) {
Console....
}


The rest api is as follow:



@GET
@Produces("text/plain")
@Path("TripExist")
public boolean isTripExist(@QueryParam("id") String id,
@QueryParam("name") String name) {
return tripDao.isTripExist(name, id);
}


I'm getting in the console an HttpResponse with the boolean value in the body but I don't know how to fetch the value and assign it to a boolean value.enter image description here










share|improve this question

























  • What'e the problem? What do you expect to happen, and what happens instead?

    – JB Nizet
    Nov 25 '18 at 10:21











  • isExist = data I would like to get a boolean value there

    – user2304483
    Nov 25 '18 at 10:22











  • As explained in the documentation (angular.io/guide/http#reading-the-full-response), and as shown in the logs you posted, data is of type HttpResponse. You get its body using... its body property. angular.io/api/common/http/HttpResponse

    – JB Nizet
    Nov 25 '18 at 10:25


















0















I have Angular 5 app with the following service call:



let isExist: boolean;
this.http.get<Boolean>(`${this.baseUrl}/Trips/TripExist`, {
headers: new HttpHeaders({ 'Accept': 'text/plain', 'Content-Type': 'text/plain' }),
params: {id: id, name: name},
observe: 'response'
}).subscribe(
data => { isExist = data.body;
console.log(data);
},
err => console. error(err)
);
if (isExist == true) {
Console....
}


The rest api is as follow:



@GET
@Produces("text/plain")
@Path("TripExist")
public boolean isTripExist(@QueryParam("id") String id,
@QueryParam("name") String name) {
return tripDao.isTripExist(name, id);
}


I'm getting in the console an HttpResponse with the boolean value in the body but I don't know how to fetch the value and assign it to a boolean value.enter image description here










share|improve this question

























  • What'e the problem? What do you expect to happen, and what happens instead?

    – JB Nizet
    Nov 25 '18 at 10:21











  • isExist = data I would like to get a boolean value there

    – user2304483
    Nov 25 '18 at 10:22











  • As explained in the documentation (angular.io/guide/http#reading-the-full-response), and as shown in the logs you posted, data is of type HttpResponse. You get its body using... its body property. angular.io/api/common/http/HttpResponse

    – JB Nizet
    Nov 25 '18 at 10:25
















0












0








0








I have Angular 5 app with the following service call:



let isExist: boolean;
this.http.get<Boolean>(`${this.baseUrl}/Trips/TripExist`, {
headers: new HttpHeaders({ 'Accept': 'text/plain', 'Content-Type': 'text/plain' }),
params: {id: id, name: name},
observe: 'response'
}).subscribe(
data => { isExist = data.body;
console.log(data);
},
err => console. error(err)
);
if (isExist == true) {
Console....
}


The rest api is as follow:



@GET
@Produces("text/plain")
@Path("TripExist")
public boolean isTripExist(@QueryParam("id") String id,
@QueryParam("name") String name) {
return tripDao.isTripExist(name, id);
}


I'm getting in the console an HttpResponse with the boolean value in the body but I don't know how to fetch the value and assign it to a boolean value.enter image description here










share|improve this question
















I have Angular 5 app with the following service call:



let isExist: boolean;
this.http.get<Boolean>(`${this.baseUrl}/Trips/TripExist`, {
headers: new HttpHeaders({ 'Accept': 'text/plain', 'Content-Type': 'text/plain' }),
params: {id: id, name: name},
observe: 'response'
}).subscribe(
data => { isExist = data.body;
console.log(data);
},
err => console. error(err)
);
if (isExist == true) {
Console....
}


The rest api is as follow:



@GET
@Produces("text/plain")
@Path("TripExist")
public boolean isTripExist(@QueryParam("id") String id,
@QueryParam("name") String name) {
return tripDao.isTripExist(name, id);
}


I'm getting in the console an HttpResponse with the boolean value in the body but I don't know how to fetch the value and assign it to a boolean value.enter image description here







angular rest httpresponse






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 11:55









SiddAjmera

15.7k31239




15.7k31239










asked Nov 25 '18 at 10:19









user2304483user2304483

8519




8519













  • What'e the problem? What do you expect to happen, and what happens instead?

    – JB Nizet
    Nov 25 '18 at 10:21











  • isExist = data I would like to get a boolean value there

    – user2304483
    Nov 25 '18 at 10:22











  • As explained in the documentation (angular.io/guide/http#reading-the-full-response), and as shown in the logs you posted, data is of type HttpResponse. You get its body using... its body property. angular.io/api/common/http/HttpResponse

    – JB Nizet
    Nov 25 '18 at 10:25





















  • What'e the problem? What do you expect to happen, and what happens instead?

    – JB Nizet
    Nov 25 '18 at 10:21











  • isExist = data I would like to get a boolean value there

    – user2304483
    Nov 25 '18 at 10:22











  • As explained in the documentation (angular.io/guide/http#reading-the-full-response), and as shown in the logs you posted, data is of type HttpResponse. You get its body using... its body property. angular.io/api/common/http/HttpResponse

    – JB Nizet
    Nov 25 '18 at 10:25



















What'e the problem? What do you expect to happen, and what happens instead?

– JB Nizet
Nov 25 '18 at 10:21





What'e the problem? What do you expect to happen, and what happens instead?

– JB Nizet
Nov 25 '18 at 10:21













isExist = data I would like to get a boolean value there

– user2304483
Nov 25 '18 at 10:22





isExist = data I would like to get a boolean value there

– user2304483
Nov 25 '18 at 10:22













As explained in the documentation (angular.io/guide/http#reading-the-full-response), and as shown in the logs you posted, data is of type HttpResponse. You get its body using... its body property. angular.io/api/common/http/HttpResponse

– JB Nizet
Nov 25 '18 at 10:25







As explained in the documentation (angular.io/guide/http#reading-the-full-response), and as shown in the logs you posted, data is of type HttpResponse. You get its body using... its body property. angular.io/api/common/http/HttpResponse

– JB Nizet
Nov 25 '18 at 10:25














1 Answer
1






active

oldest

votes


















0














I'm not sure why you're passing the observe option there. I'm assuming that you want to read some headers or some other meta data on the response. Keeping that in mind, since you've done { observe: 'response' }, you'll get the complete Response object with a lot of fields in it. But all you're concerned about is the body field.



So you can read it like this:



let isExist: boolean;

this.http.get(`${this.baseUrl}/Trips/TripExist`, {
headers: new HttpHeaders({
'Accept': 'text/plain',
'Content-Type': 'text/plain'
}),
params: {
id: id,
name: name
},
observe: 'response'
}).subscribe(
data => {
isExist = data.body; // HERE data.body will have the boolean that you're looking for.
console.log(data);
console.log(isExist);
if (isExist === true) { console.... }
},
err => console.error(err)
);


UPDATE:



It won't work if the if condition is outside the subscribe block. The code inside the subscribe block runs asynchronously, i.e. after the API call is done with and the response is received. But the if condition would run synchronously, i.e. before the subscribe block. So when the control reaches your if condition, the isExist variable would still be undefined as it hasn't been initialized and only gets initialized inside the subscribe block which runs AFTER the if condition is executed.



I've updated my answer with the if condition moved inside the subscribe block.






share|improve this answer


























  • I’m not require the full response so I guess I can remove the observe option. And I’ve tried to access the value using the data.body but I couldn’t use it inside an IF expression. If (data.body == true) or if (data.body == ‘true’) both didn’t work

    – user2304483
    Nov 25 '18 at 11:14











  • When printing data.body to the console I got true but it didn’t work in an IF statement from some reason.

    – user2304483
    Nov 25 '18 at 11:23











  • Ive update the question with the IF statement. It coming exactly after the service call.

    – user2304483
    Nov 25 '18 at 11:41











  • Sorry, I’m using the mobile now so can’t do the correct indent.

    – user2304483
    Nov 25 '18 at 11:49











  • The if statement is after the subscribe block.

    – user2304483
    Nov 25 '18 at 11:49











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',
autoActivateHeartbeat: false,
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%2f53466539%2fhow-to-fetch-a-boolean-value-from-the-result-in-angular-http-client-angular-5%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









0














I'm not sure why you're passing the observe option there. I'm assuming that you want to read some headers or some other meta data on the response. Keeping that in mind, since you've done { observe: 'response' }, you'll get the complete Response object with a lot of fields in it. But all you're concerned about is the body field.



So you can read it like this:



let isExist: boolean;

this.http.get(`${this.baseUrl}/Trips/TripExist`, {
headers: new HttpHeaders({
'Accept': 'text/plain',
'Content-Type': 'text/plain'
}),
params: {
id: id,
name: name
},
observe: 'response'
}).subscribe(
data => {
isExist = data.body; // HERE data.body will have the boolean that you're looking for.
console.log(data);
console.log(isExist);
if (isExist === true) { console.... }
},
err => console.error(err)
);


UPDATE:



It won't work if the if condition is outside the subscribe block. The code inside the subscribe block runs asynchronously, i.e. after the API call is done with and the response is received. But the if condition would run synchronously, i.e. before the subscribe block. So when the control reaches your if condition, the isExist variable would still be undefined as it hasn't been initialized and only gets initialized inside the subscribe block which runs AFTER the if condition is executed.



I've updated my answer with the if condition moved inside the subscribe block.






share|improve this answer


























  • I’m not require the full response so I guess I can remove the observe option. And I’ve tried to access the value using the data.body but I couldn’t use it inside an IF expression. If (data.body == true) or if (data.body == ‘true’) both didn’t work

    – user2304483
    Nov 25 '18 at 11:14











  • When printing data.body to the console I got true but it didn’t work in an IF statement from some reason.

    – user2304483
    Nov 25 '18 at 11:23











  • Ive update the question with the IF statement. It coming exactly after the service call.

    – user2304483
    Nov 25 '18 at 11:41











  • Sorry, I’m using the mobile now so can’t do the correct indent.

    – user2304483
    Nov 25 '18 at 11:49











  • The if statement is after the subscribe block.

    – user2304483
    Nov 25 '18 at 11:49
















0














I'm not sure why you're passing the observe option there. I'm assuming that you want to read some headers or some other meta data on the response. Keeping that in mind, since you've done { observe: 'response' }, you'll get the complete Response object with a lot of fields in it. But all you're concerned about is the body field.



So you can read it like this:



let isExist: boolean;

this.http.get(`${this.baseUrl}/Trips/TripExist`, {
headers: new HttpHeaders({
'Accept': 'text/plain',
'Content-Type': 'text/plain'
}),
params: {
id: id,
name: name
},
observe: 'response'
}).subscribe(
data => {
isExist = data.body; // HERE data.body will have the boolean that you're looking for.
console.log(data);
console.log(isExist);
if (isExist === true) { console.... }
},
err => console.error(err)
);


UPDATE:



It won't work if the if condition is outside the subscribe block. The code inside the subscribe block runs asynchronously, i.e. after the API call is done with and the response is received. But the if condition would run synchronously, i.e. before the subscribe block. So when the control reaches your if condition, the isExist variable would still be undefined as it hasn't been initialized and only gets initialized inside the subscribe block which runs AFTER the if condition is executed.



I've updated my answer with the if condition moved inside the subscribe block.






share|improve this answer


























  • I’m not require the full response so I guess I can remove the observe option. And I’ve tried to access the value using the data.body but I couldn’t use it inside an IF expression. If (data.body == true) or if (data.body == ‘true’) both didn’t work

    – user2304483
    Nov 25 '18 at 11:14











  • When printing data.body to the console I got true but it didn’t work in an IF statement from some reason.

    – user2304483
    Nov 25 '18 at 11:23











  • Ive update the question with the IF statement. It coming exactly after the service call.

    – user2304483
    Nov 25 '18 at 11:41











  • Sorry, I’m using the mobile now so can’t do the correct indent.

    – user2304483
    Nov 25 '18 at 11:49











  • The if statement is after the subscribe block.

    – user2304483
    Nov 25 '18 at 11:49














0












0








0







I'm not sure why you're passing the observe option there. I'm assuming that you want to read some headers or some other meta data on the response. Keeping that in mind, since you've done { observe: 'response' }, you'll get the complete Response object with a lot of fields in it. But all you're concerned about is the body field.



So you can read it like this:



let isExist: boolean;

this.http.get(`${this.baseUrl}/Trips/TripExist`, {
headers: new HttpHeaders({
'Accept': 'text/plain',
'Content-Type': 'text/plain'
}),
params: {
id: id,
name: name
},
observe: 'response'
}).subscribe(
data => {
isExist = data.body; // HERE data.body will have the boolean that you're looking for.
console.log(data);
console.log(isExist);
if (isExist === true) { console.... }
},
err => console.error(err)
);


UPDATE:



It won't work if the if condition is outside the subscribe block. The code inside the subscribe block runs asynchronously, i.e. after the API call is done with and the response is received. But the if condition would run synchronously, i.e. before the subscribe block. So when the control reaches your if condition, the isExist variable would still be undefined as it hasn't been initialized and only gets initialized inside the subscribe block which runs AFTER the if condition is executed.



I've updated my answer with the if condition moved inside the subscribe block.






share|improve this answer















I'm not sure why you're passing the observe option there. I'm assuming that you want to read some headers or some other meta data on the response. Keeping that in mind, since you've done { observe: 'response' }, you'll get the complete Response object with a lot of fields in it. But all you're concerned about is the body field.



So you can read it like this:



let isExist: boolean;

this.http.get(`${this.baseUrl}/Trips/TripExist`, {
headers: new HttpHeaders({
'Accept': 'text/plain',
'Content-Type': 'text/plain'
}),
params: {
id: id,
name: name
},
observe: 'response'
}).subscribe(
data => {
isExist = data.body; // HERE data.body will have the boolean that you're looking for.
console.log(data);
console.log(isExist);
if (isExist === true) { console.... }
},
err => console.error(err)
);


UPDATE:



It won't work if the if condition is outside the subscribe block. The code inside the subscribe block runs asynchronously, i.e. after the API call is done with and the response is received. But the if condition would run synchronously, i.e. before the subscribe block. So when the control reaches your if condition, the isExist variable would still be undefined as it hasn't been initialized and only gets initialized inside the subscribe block which runs AFTER the if condition is executed.



I've updated my answer with the if condition moved inside the subscribe block.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 11:54

























answered Nov 25 '18 at 10:46









SiddAjmeraSiddAjmera

15.7k31239




15.7k31239













  • I’m not require the full response so I guess I can remove the observe option. And I’ve tried to access the value using the data.body but I couldn’t use it inside an IF expression. If (data.body == true) or if (data.body == ‘true’) both didn’t work

    – user2304483
    Nov 25 '18 at 11:14











  • When printing data.body to the console I got true but it didn’t work in an IF statement from some reason.

    – user2304483
    Nov 25 '18 at 11:23











  • Ive update the question with the IF statement. It coming exactly after the service call.

    – user2304483
    Nov 25 '18 at 11:41











  • Sorry, I’m using the mobile now so can’t do the correct indent.

    – user2304483
    Nov 25 '18 at 11:49











  • The if statement is after the subscribe block.

    – user2304483
    Nov 25 '18 at 11:49



















  • I’m not require the full response so I guess I can remove the observe option. And I’ve tried to access the value using the data.body but I couldn’t use it inside an IF expression. If (data.body == true) or if (data.body == ‘true’) both didn’t work

    – user2304483
    Nov 25 '18 at 11:14











  • When printing data.body to the console I got true but it didn’t work in an IF statement from some reason.

    – user2304483
    Nov 25 '18 at 11:23











  • Ive update the question with the IF statement. It coming exactly after the service call.

    – user2304483
    Nov 25 '18 at 11:41











  • Sorry, I’m using the mobile now so can’t do the correct indent.

    – user2304483
    Nov 25 '18 at 11:49











  • The if statement is after the subscribe block.

    – user2304483
    Nov 25 '18 at 11:49

















I’m not require the full response so I guess I can remove the observe option. And I’ve tried to access the value using the data.body but I couldn’t use it inside an IF expression. If (data.body == true) or if (data.body == ‘true’) both didn’t work

– user2304483
Nov 25 '18 at 11:14





I’m not require the full response so I guess I can remove the observe option. And I’ve tried to access the value using the data.body but I couldn’t use it inside an IF expression. If (data.body == true) or if (data.body == ‘true’) both didn’t work

– user2304483
Nov 25 '18 at 11:14













When printing data.body to the console I got true but it didn’t work in an IF statement from some reason.

– user2304483
Nov 25 '18 at 11:23





When printing data.body to the console I got true but it didn’t work in an IF statement from some reason.

– user2304483
Nov 25 '18 at 11:23













Ive update the question with the IF statement. It coming exactly after the service call.

– user2304483
Nov 25 '18 at 11:41





Ive update the question with the IF statement. It coming exactly after the service call.

– user2304483
Nov 25 '18 at 11:41













Sorry, I’m using the mobile now so can’t do the correct indent.

– user2304483
Nov 25 '18 at 11:49





Sorry, I’m using the mobile now so can’t do the correct indent.

– user2304483
Nov 25 '18 at 11:49













The if statement is after the subscribe block.

– user2304483
Nov 25 '18 at 11:49





The if statement is after the subscribe block.

– user2304483
Nov 25 '18 at 11:49




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466539%2fhow-to-fetch-a-boolean-value-from-the-result-in-angular-http-client-angular-5%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

Costa Masnaga

Fotorealismo

Sidney Franklin