The issue of returning a boolean value in an rxjs function leading to failure

Hey there, I am currently learning about rxjs and I want to create an observable that returns either true or false.

This is my attempted code:

checkLoggedIn(): Observable<boolean> {
  // Check with the server if the user is logged in
  if(this._tokenService.getToken()){ 
    // This function returns synchronous true or false

      this._httpservice.checkIfLoggedIn()
         .subscribe((res) => {
            return res.data; // This value will be true or false
              },
              err => { return false; }
              )    
     } else {
        return new Observable(observer => {
          observer.next(false);
          observer.complete();
        });
    }

 }

How can I modify the 'else' part above to ensure it works with the boolean observable so that in other components I can simply do:

this._authservice.checkLoggedIn()
  .subscribe(res => console.log(res));

Answer №1

everything seems to be functioning properly

verifyUserSession():Observable<boolean> { 
  if(this._tokenService.getToken()){ 
    return this._httpservice.checkSessionStatus().map(res=>res.success ) ;
  } else { 
    return Observable.of(false);
  }
}

Answer №2

Opt for return Observable.of(false) over using return false.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Modify the name of the variable when sending the object to an HTTP service in Angular version 6

export class ShopModel { public id: number; public name: string; public email: string; public phone: string; public website: string; public address: string; public gst_number: string; public pan_number: string; public ta ...

The modal functionality in AngularJS doesn't seem to be functioning properly

I am currently working on an Angular application where I want to implement a button that, when clicked, will open a pop-up window displaying a chart. Below is the button code: <div style="padding-top:50px;padding-left:10px;"> <button type="butto ...

Unassigned variable in need of initialization within Angular 2

There seems to be an issue with the two-way data binding not functioning correctly. I am trying to retrieve the data using {user | json}, but I encounter an error when using [(ngModel)] = "user.username". Data Model export interface UserModel { ...

Creating a React component with a column allowing multiple checkbox selections in NextUI table

Setting up multiple "checkbox" columns in a table using the NextUI table has been my current challenge. Each row should have selectable checkboxes, and I want these selections to be remembered when navigating between pages, running searches, or removing co ...

What is the process by which Angular2+ ngFor directive cycles through the ref iterable?

My curiosity led me to dive into the inner workings of the Angular *ngFor directive. I was particularly interested in understanding how Angular interprets the iterable object passed to the directive - whether it re-evaluates it at each loop iteration simil ...

Encountered an error while attempting to compare 'true' within the ngDoCheck() function in Angular2

Getting Started Greetings! I am a novice in the world of Angular2, Typescript, and StackOverflow.com. I am facing an issue that I hope you can assist me with. I have successfully created a collapse animation for a button using ngOnChanges() when the butto ...

Which is better: Utilizing ASP.NET Core or ASP.NET Core MVC in tandem with Angular for the

Currently in the planning stages of developing a website or web application using .NET Core for the backend and Angular for the frontend. One aspect that is proving to be confusing is whether to use ASP.NET Core or ASP.NET Core MVC on the backend. I'm ...

Faulty deduction can occur when implementing the return statement in an identity function, or when incorporating an optional parameter

Encountering strange behavior while working on identity functions in a wizard system schema. Using a constrained identity function for inference is causing issues with one property that cannot be inferred when using the following: When the return value fr ...

Angular production application is experiencing issues due to a missing NPM package import

Objective I am aiming to distribute a TypeScript module augmentation of RxJS as an npm package for usage in Angular projects. Challenge While the package functions correctly in local development mode within an Angular application, it fails to import pro ...

The functionality of connect-flash in Express JS is compromised when used in conjunction with express-mysql-session

I am facing a unique issue in my project. I have identified the source of the problem but I am struggling to find a solution. My project utilizes various modules such as cookie-parser, express-mysql-session, express-session, connect-flash, passport and m ...

Having trouble implementing catchError in a unit test for an HttpInterceptor in Angular 11

I am facing challenges in completing a unit test for my HttpInterceptor. The interceptor serves as a global error handler and is set to trigger on catchError(httpResponseError). While the interceptor functions perfectly fine on my website, I am struggling ...

Utilize components with dynamic identifiers for enhanced versatility - Angular 4

Is it possible to use the map.component within my dynamic window.component multiple times in different windows? Each time a click event triggers the creation of a new dynamic window with a map component inside. Here's an example: window.component.htm ...

Can Angular Universal be configured to have all HTTP Requests originate from the Server instead of the Client's Browser in an Angular Application?

With specific requirements in mind, I require the Angular application to carry out all requests on the server rather than on the client's browser. After implementing SSR (Server-Side Rendering) through Angular Universal on my application, I anticipat ...

Unleashing the power of Typescript enums in conjunction with external modules through browserify

Previously, I utilized TypeScript internal modules and included numerous script tags to initialize my app. Now, I am in the process of transitioning the project to utilize external modules (using browserify), but I have hit a roadblock when it comes to con ...

Variables for Angular Material themes

My app has multiple theme files, and I select the theme during runtime. .client1-theme{ // @include angular-material-theme($client1-theme); @include all-component-colors($client1-theme); @include theme-color-grabber($client1-theme, $client1-a ...

Encountering a challenge when upgrading to eslint version 9.0.0

Encountering an issue while trying to upgrade eslint to version 9.0.0. ⋊> ~/A/fusion on turborepo ⨯ bun lint 22:21:58 $ eslint packages/*/src/**/* Oops! Something went wrong! :( ESLint: 9.0. ...

Remove validators from an Angular formArray

Is there a way to reset validators for the formArray within my formGroup? Check out my StackBlitz for reference. .ts this.createEstimation = this.fb.group({ name: ['', Validators.required], surname: ['', Validators.required], ca ...

Retrieve data from Ionic storage

Need help with Ionic storage value retrieval. Why is GET2 executing before storage.get? My brain needs a tune-up, please assist. public storageGet(key: string){ var uid = 0; this.storage.get(key).then((val) => { console.log('GET1: ' + key + ...

Showing the chosen dropdown option using formControlName in ReactiveForms

I have a dropdown menu with validation using formControlName. I also wanted to display the selected option from the dropdown. To achieve this, I used ngModel. However, I encountered an error message stating: It seems that you are using ngModel on the same ...

Tips on displaying hyperlinks within a text area in an Angular application

In my Angular application, I am facing an issue with displaying hyperlinks within a text area box for dynamic content. The hyperlinks are not recognized and therefore cannot be clicked. Can someone please advise on how to properly display hyperlinks with ...