I can't figure out why I keep getting an error when I try to subscribe to an Observable after using the RxJS tap() operator

Currently, I am facing an issue while working on my Angular application that utilizes Firebase Firestore database and involves the use of RxJS. Let me elaborate on the problem I encountered.

Within a service class, I have implemented the following method:

  getPatientAesteticEvaluationData(patientUID): Observable<AestheticEvaluation> {
    return this.firestore.collection('aesthetic-evaluation')
    .doc(patientUID)
    .valueChanges()
    .pipe(
      map(aestheticEvaluation => !!aestheticEvaluation ? aestheticEvaluation : {}),
      tap(console.log)
    )
  }

This method aims to fetch data from a Firestore collection, if it exists, and return it as an Observable.

In the component code, I have the following implementation:

  ngOnInit(): void {
    console.log("AestheticEvaluationComponent INIT");

    //this.patientAestheticEvaluationData$ = this.patientService.getPatientAesteticEvaluationData(this.patientUID)
    this.patientAestheticEvaluationData$ = this.patientService.getPatientAesteticEvaluationData(this.patientUID)
        .pipe(

          tap(aestheticEvaluationData => !!aestheticEvaluationData ? this.fillAestheticEvaluationForm(aestheticEvaluationData) : null),
          tap(console.log)
        );

    //this.patientAestheticEvaluationData$.subscribe();
  }

I aim to invoke the service method to receive the returned Observable object. Next, I attempt to access the content of this object to call the fillAestheticEvaluationForm(aestheticEvaluationData) function with this data.

As of now, the only content within my fillAestheticEvaluationForm() method is:

  fillAestheticEvaluationForm(aestheticEvaluationData) {
    console.log("fillAestheticEvaluationForm START !!! aestheticEvaluationData: ", aestheticEvaluationData);

  }

The issue lies in the fact that this method is never invoked. I believe that I need to subscribe to the returned observable, but when I attempt to do so like this:

this.patientAestheticEvaluationData$ = this.patientService.getPatientAesteticEvaluationData(this.patientUID)
    .pipe(

      tap(aestheticEvaluationData => !!aestheticEvaluationData ? this.fillAestheticEvaluationForm(aestheticEvaluationData) : null),
      tap(console.log)
    ).subscribe();

An error occurs with this.patientAestheticEvaluationData$:

ERROR in src/app/features/patient/patient-details/aesthetic-evaluation/aesthetic-evaluation.component.ts:24:5 - error TS2740: Type 'Subscription' is missing the following properties from type 'Observable<AestheticEvaluation>': _isScalar, source, operator, lift, and 6 more.

24     this.patientAestheticEvaluationData$ = this.patientService.getPatientAesteticEvaluationData(this.patientUID)

What could be the issue here? What am I overlooking? And how can I rectify this code?

Answer №1

When you subscribe to an Observable, it transforms into a Subscription rather than remaining as an Observable. Thus, in your component, make sure to subscribe to the Observable or return void if no data needs to be returned.

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

Incorporating new element into a Typescript array

I'm currently working on a project using TypeScript and MongoDB where I need to insert an item into an array. registeredUsers.update({ guid: ObjectId(req.cookies._id) }, { $addToSet: { favorites: [comicID] } }); The code snippet prov ...

Issue encountered while utilizing npm on Visual Studio 2015

I'm attempting to run my Angular2 project with Visual Studio 2015. It works perfectly when I start it using npm in the windows console with the command 'npm start'. However, when trying to do the same thing using npm Task Runner for VS, I e ...

Is ngForIn a valid directive in Angular 4?

While attempting to loop over the properties of an object using *ngFor with in, I encountered a challenge. Here is a sample code snippet: @Controller({ selector: 'sample-controller', template: ` <ul> <li *ngFor="let i in o ...

Unable to utilize Angular object due to the JSON format of the data

I'm facing an issue in my Angular 4 application with TypeScript. I am using a get() method and a subscribe() method to receive a remote object in JSON format and deserialize it to match a class structure. When all the class data fields are mirrored in ...

Animating table changes in Angular Material using a subscription

Let me illustrate my goal with an example: I currently have an Angular material table that is populating data from a class that extends DataSource. The connect method in the DataSource is subscribing to data from sources like Firestore. Whenever there ar ...

Angular - organizing information together

After retrieving the data from the database using JSON, here is the format in which it was received. var data = [ { "view_id": "999999999", "month" : 01, "year" : 2017, "visit" : 2000 }, { "view_id": "999999999", "month" ...

Creating a unique syntax for custom ngIf directives in Angular

Currently, I am in the process of developing a personalized *ngIf directive that will swap out content with a placeholder during loading. After referencing the *ngIf directive (https://github.com/angular/angular/blob/master/packages/common/src/directives/n ...

Issue: the function this.infoWindowFunction is not defined

Attempting to make a function call with parameters is giving me the error mentioned in the subject. The function declaration and call should be correct based on how I'm doing it below. I have tried the following methods but none seem to work: infoW ...

Angular 10: How to Retrieve NgForm from Projected Content

Imagine I have a component structured like this: <app-my-typical-form> <app-child-in-content></app-child-in-content> </app-my-typical-form> where the code for my-typical-form.component.html is as follows: <form #f="ngForm"&g ...

Create a package themed with Material UI for export

I am attempting to create a new npm package that exports all @material-ui/core components with my own theme. I am currently using TypeScript and Rollup, but encountering difficulties. Here is the code I have: index.ts export { Button } from '@materia ...

Is there a way to leverage the extensive Bootstrap 5 color palette within an Angular application?

Currently working on a project in Angular 12 with Bootstrap 5 and SCSS, I'm facing a challenge in utilizing the new extended Bootstrap 5 color palette such as indigo-300 or pink-200. I'm not sure if I need to import them in a certain way or how t ...

Mastering the art of incorporating objects into templates using *ngFor Angular

Whenever I do my *ngFor loop in my HTML template, the data is displaying as [Object Object]. Below is my view with the enumerated data in the developer console: https://i.stack.imgur.com/KXmiI.png This is the snippet of my HTML code: https://i.stack.im ...

Securing your Angular application with user authentication and route guarding ensures

In the process of developing an Angular single-page application (SPA) front-end that interacts with a GraphQL endpoint, I encountered a challenge. Upon user login, I store the token in local storage and update the authentication state in my AuthService com ...

What is the most effective method for designing a scalable menu?

What is the most effective way to create a menu similar to the examples in the attached photos? I attempted to achieve this using the following code: const [firstParentActive, setFirstParentActive] = useState(false) // my approach was to use useState for ...

Code in Javascript to calculate the number of likes using Typescript/Angular

I have encountered a challenge with integrating some JavaScript code into my component.ts file in an Angular project. Below is the code snippet I am working on: ngOninit() { let areaNum = document.getElementsByClassName("some-area").length; // The pr ...

The argument specified as 'Blob | Blob[]' cannot be assigned to a parameter expecting just a 'Blob'

I've been working on this code snippet: const heicReader = new FileReader(); heicReader.onload = async () => { const heicImageString = heicReader.result; const { download_url } = await uploadPhotoToGcs({ ...

Using TypeScript with AWS Lambda: To package imports or not to package? Alternatively: Error in Runtime.ImportModule: Module '@aws-sdk/...' not found

I have been working with the code in my lambda.ts file, attempting to execute it on an AWS Lambda: import 'aws-sdk' import { /* bunch of stuff... */ } from "@aws-sdk/client-cloudwatch-logs"; import {Context, APIGatewayProxyResult} from ...

Creating a TypeScript type that can verify the validity of a hexadecimal string value

For example: let hexStr: string = "01afe3"; // valid let hexStr1: string = "0a1" // invalid, hex string length should be even let hexStr2: string = "hello" //invalid, only hex characters allowed The string value must be a valid hexadecimal. I attempted t ...

Issues with CSS styling inheritance in Angular components

I'm facing an issue with a button that is part of an InfoWindow component. The button is not created in the HTML code directly but is called whenever the card component opens. I have integrated this InfoCard into two different sections of the applicat ...

Activate Angular Material's autocomplete feature once the user has entered three characters

My goal is to implement an Angular Material Autocomplete feature that only triggers after the user has inputted at least three characters. Currently, I have it set up so that every click in the input field prompts an API call and fetches all the data, whic ...