3

HttpClient not parsing Boolean value

Service

 public isUSCustomer(): Observable<Boolean> {

  return this.httpClient
        .get<Boolean>(myurl);

}

Component

private isUScustomer: Boolean; 
this.myservice.isUSCustomer()
              .subscribe(x => {
                   this.isUScustomer = x;
                   console.log(this.isUScustomer); //Its undefined 
                   console.log(x); //it logs true.
});

Outputs

console.log(this.isUScustomer); undefined
console.log(x); //it logs true.
console.log(typeof x); boolean

I tried with Interface Boolean and boolean like this.

return this.httpClient
        .get<boolean>(myurl);

and

return this.httpClient
        .get<Boolean>(myurl);

Api Result

true

I read this Typescript boolean conversion but it was 2013.

Version Info

typescript: 2.4.2
Angular: 5.0.2

6
  • What does console.log(typeof x) returns ? Commented Dec 14, 2017 at 14:30
  • it logged boolean Commented Dec 14, 2017 at 14:31
  • Well, it's strange. First don't use the interface, only use the primitive type. Could you try with this.isUScustomer = x ? true : false; ? Commented Dec 14, 2017 at 14:32
  • What does console.log(x) return? true or 'true'? Commented Dec 14, 2017 at 14:43
  • it return true value Commented Dec 14, 2017 at 14:44

4 Answers 4

2

Well I had seen a same error few days back I stumbled upon something called eval function, that is how you can perhaps try this running,

private isUScustomer: Boolean; 
this.myservice.isUSCustomer()
              .subscribe(x => {
                   this.isUScustomer = eval(x);
});

Edit 1

TS look for return type and datatype for http calls, you can perhaps try to create an interface and give this a go. Let me show you how,

an Interface like this,

export interface booleanReturn{
    retData: boolean;
}

Import this in your service and use it as given below,

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { HttpBackend } from '@angular/common/http/src/backend';
import { booleanReturn } from '../interfaces/MyInterfaces';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class MyServiceService {

  constructor(private httpClient: HttpClient) { }

  getBoolean(): Observable<booleanReturn>{
    return this.httpClient.get<booleanReturn>('http://localhost:8080/getBoolean');
  }
}

Now in your component do something like this,

import { Component } from '@angular/core';
import { MyServiceService } from './my-service.service';
import { booleanReturn } from '../interfaces/MyInterfaces';

/*interface booleanReturn{
  retData: boolean
}*/
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';
  myObj ={"decision":'2==2'};
  private myBooleanVal : booleanReturn;
  constructor(private myService:MyServiceService){
    this.getBooleanValue();
  }
  myFunction(vwLog){
    //console.log(eval(vwLog),"Dj test");
    return eval(vwLog);
  }

  getBooleanValue(){
    this.myService.getBoolean().subscribe(x=>{
      this.myBooleanVal = x;
      console.log(x,"X Value");// this print true "X Value"
      console.log(this.myBooleanVal);// this prints "true"
    });
  }
}

see my screenshot below I can see the correct answer

enter image description here

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

12 Comments

Let me try it, but i have in service ask to evaluate get<boolean>(url) . should it parse to a boolean
yeh it should be but, give this a go, I was able to sort my problem of boolean using this.
In my case it was an error or string type and boolean type, somehow I think due to dynamic casting it was not working and accepting it as a string even when mentioned as boolean.
@Eldho any luck?
PLEASE don't do EVAL. Is considered harmful.
|
1

I suspect that "this" is out of context when the subscribe happens.

Try adding your code into the constructor:

private isUScustomer: Boolean; 


constructor() {
  this.myservice.isUSCustomer()
              .subscribe(x => {
                   this.isUScustomer = x;
                   console.log(this.isUScustomer); //Its undefined 
                   console.log(x); //it logs true.
  });
}

1 Comment

I will try this but my im using different methods in the service in same manner with complex object it works fine.
0

use Let instead of private and return that variable

1 Comment

I dont think it because of private. Because i consume other services like the same with complex object which works fine.
0

If you are using Boolean type you have to initialize it with a default value before setting it .

private isUScustomer: Boolean = false; 

Another alternative will be to use boolean type

private isUScustomer: boolean;

2 Comments

private isUScustomer: Boolean = false should have worked .. whats the final isUsCustomer value ;
But i havent set a value ; i just defined the property like isuscustomer:boolean;

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.