Manipulate and send back information from Observable in Angular

Here is an example of a scenario where I have created a service to retrieve a list of properties. Within this service, I am utilizing the map function to manipulate the response and then returning the final array as an Observable.

My question is: How can I easily obtain that final array in JSON format from the service and pass it to my component? Below is what I have accomplished thus far:

SERVICE

getPropertyList(){
    let propertyList$ : Observable<any> = this._http.get('http://localhost:3000/...');

    return propertyList$.pipe(
      map(data => {
        // perform necessary transformations
        let listArray: Array<any> = data.response.dataset;
        listArray.forEach(list => {
          list.name = 'John Doe';
        })
        return listArray;
      })
    )
}

COMPONENT

this._service.getPropertyList()
 .subscribe({
   next: (result) => {
     console.log(result);
   },
   error: (err) => {
     console.log(err);
   }
})

In my component, I am attempting to achieve the desired result like so -

getData(){
   const listData: Array<any> = this._service.getPropertyList();
}

Answer №1

When working with observables, the initial instinct may be to extract the data and store it elsewhere. However, this is often unnecessary. Instead, utilizing pipeable operators can effectively design the behavior and data transformation of emitted values.

Instead of retrieving data in your component using a method:

getData() {
   const listData: Array<any> = this._service.getPropertyList();
}

You could declare the data as an observable that automatically provides the data when available:

data$: Observable<Array<any>> = this._service.getPropertyList();

By taking this approach, your component becomes simpler as it no longer needs to handle fetching the data directly. Instead, it references the observable which will update with any changes to the data.

The $ serves as an indicator that this is a "value that is automatically updated" (observable).

Rather than subscribing in your component as before:

this._service.getPropertyList()
 .subscribe({
   next: (result) => {
     console.log(result);
   },
   error: (err) => {
     console.log(err);
   }
})

You can simplify by defining your data$ as an observable and utilizing Angular's async pipe for subscription handling directly from the template:

data$ = this._service.getPropertyList().pipe(
   tap(result => console.log(result)),
   catchError(err => console.log(err))
});
<ul>
  <li *ngFor="let item of data$ | async">{{ item.name }}</li>
</ul>

Key Operators Used:

  • tap - Executes side effect logic without changing emitted values
  • catchError - Handles errors within the observable stream

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

Arrangement of items in Angular 2 array

Received a JSON response structured like this JSON response "Terms": [ { "Help": "Terms", "EventType": "Success", "Srno": 1, "Heading": "Discount Condition", "T ...

Having trouble getting a local npm installation to work from a specific file path?

After following the instructions from this helpful link to install an npm package through a file path, I encountered an error when attempting to use it: Cannot find module '<module_name>' or its corresponding type declaration Are there an ...

The Mat-Datepicker will always show the current date in your local time zone, never in

Utilizing a datepicker and momentjs, I have successfully implemented sending UTC dates to my server. However, the issue arises when retrieving these UTC dates back into the date picker, as it always reflects my timezone (EST). This discrepancy can result i ...

Can one assess a private method within a class?

Within my Ionic 3 (Angular 4) application, I am working with the following code snippet: this.$observable.subscribe(success => { if (success) { this.toastCtrl.create({ message: 'message, duration: 3000, position: 'midd ...

Preparing an Angular 2 application for deployment on a live IIS server

After following the Angular 2 tutorial successfully and getting everything to work as expected, I realized that it doesn't cover packaging for production distribution. Is there a standard method for creating a clean distribution package without includ ...

Function 'Once' in Typescript with Generics

Currently, I am utilizing a feature called Once() from FP. In TypeScript, I need to define the types for this function but have been struggling with the implementation. Here's what I have attempted so far: const once = <T, U>(fn: (arg: T) => ...

The language in the React Native app remains unchanged despite utilizing i18next.changeLanguage

I am currently attempting to integrate the i18next library into my React Native app in order to facilitate language changes, but I have encountered difficulties with translation. image description here I have created an i18n.tsx file. import i18next from & ...

What is the best way to preserve an enumeration value in TypeScript?

Is there a way to save enumeration values in TypeScript? For instance: createArticle(name: string, clr: ??enumeration??) { return axios.post(`${environment.apiUrl}/cards`, { card: `${name}`, color: ??clr?? }, ... } PS: Conte ...

Unable to navigate beyond the boundaries of an app

I am currently facing an issue with my authentication service where it redirects to the identity server in case of certain errors. I attempted to achieve this using window.location.href = environment.authCodeFlowIssuer;, however, an error occurred: Ref ...

The Typescript Select is displaying an incorrect value

Here's the code snippet I've been working with: <select #C (change)="changeSelect(zone.id, C.value)"> <option *ngFor="let town of townsLocal" [attr.value]="town.data" [attr.selected]="town.data === zone.town && 'selected& ...

Discovering React Styled Components Within the DOM

While working on a project using Styled Components in React, I have successfully created a component as shown below: export const Screen = styled.div({ display: "flex", }); When implementing this component in my render code, it looks like this ...

Implementing a feature in Typescript/React component that provides autocomplete functionality

Currently, I have developed a TypeScript and React component that has been published on NPM. My goal is to enable IntelliSense to autocomplete React props for this component. While I typically use JSDoc for plain React components, it does not seem to work ...

The TypeScript compiler generates a blank JavaScript file within the WebStorm IDE

My introduction to TypeScript was an interesting experience. I decided to convert a simple JavaScript application, consisting of two files, into TypeScript. The first file, accounts.ts, contains the main code, while the second one, fiat.ts, is a support f ...

Select one of 2 parameters and begin typing

I recently encountered a situation where I needed to define a type with an id field (string) and an oldId field (number), but I wanted these fields to be exclusive. For example: { id: "1234", name: "foo" } { oldId: 1234, name: "b ...

Locating the CSS selector for Material-UI combobox options in Playwright

Currently, I am struggling to identify the CSS selector for the items listed under a mui Combobox. The main issue is that each item is dynamically identified by the 'aria-activedescendant' attribute, which takes on values like id-option-0, id-opt ...

Angular: encountering template parse errors with unknown <component> element

I'm struggling to import a component into another component, but the imported component cannot be found. Here is the error message: Uncaught Error: Template parse errors: 'aktenkorrespondenzenTemplate' is not a known element: 1. If 'ak ...

Adjust the dimensions of an Angular Material 2 dialog by updating the width or height

Is there a way to adjust the dimensions of an open Angular Material 2 dialog, either its width or height? I attempted to modify the size of the dialog by obtaining a reference to it and using the updateSize method within the dialog. Unfortunately, I belie ...

The reason the CSS direct descendant selector does not affect Angular components

We are working with a basic main.html. <app> <sidebar></sidebar> <main-content> <router-outlet></router-outlet> </main-content> </app> After loading a component through routing (changing the ...

CoursesComponent does not contain a Directive annotation

I have been following a tutorial online at this link: https://www.youtube.com/watch?v=_-CD_5YhJTA Unfortunately, I keep encountering the following error message: EXCEPTION: No Directive annotation found on CoursesComponent Here is an excerpt from my a ...

Explore the capabilities of the Angular Ng2SearchPipeModule to enhance your search input

I used the ng2SearchPipeModule for an input search, but it's not working. I can't seem to find my error. In my HTML, all my books are within divs. Will typing a book title display all the divs? Of course, I have imported in app.module.ts Ng2Sear ...