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

3
  • What'e the problem? What do you expect to happen, and what happens instead? Commented Nov 25, 2018 at 10:21
  • isExist = data I would like to get a boolean value there Commented Nov 25, 2018 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 Commented Nov 25, 2018 at 10:25

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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
When printing data.body to the console I got true but it didn’t work in an IF statement from some reason.
Ive update the question with the IF statement. It coming exactly after the service call.
Sorry, I’m using the mobile now so can’t do the correct indent.
The if statement is after the subscribe block.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.