Returned boolean value in rxjs function is not executed

I am new to rxjs and I would like to return the observable to true or false

here is what i tried

checkLoggedin():Observable<boolean> {
  //check from server if user is loggdin
  if(this._tokenService.getToken()){ //this returns synchronous true or false

      this._httpservice.checkifloggedin()
         .subscribe((res)=>{
            return res.data //this comes in as true or false value
              },
              err=>{ return false }
              )    
     }else{

        return false  //this fails with an error of 
                    //type false is not assignable to observable<boolean>
        }

 }

      

How do I modify the above part to work with a boolean observable so that in other components I can only do

this._authservice.checkLoggedin()
  .subscribe.....//here get the value whether true or false

      

+3


source to share


2 answers


Use return Observable.of(false)

instead return false

.



+2


source


this should work



checkLoggedin():Observable<boolean> { 
  if(this._tokenService.getToken()){ 
    return this._httpservice.checkifloggedin().map(res=>res.data ) ;
  } else { 
    return Observable.of(false);
  }
}

      

+5


source







All Articles