Mapping the response from an http.get call to create a new typed object instance in Angular 2

Can anyone help me understand how to map the result from a service call to an object using http.get and Observables in Angular 2?

Please check out this example

In my getPersonWithGetProperty method, I am trying to return an Observable of type PersonWithGetProperty. However, I am unable to access the property fullName. It seems like I need to create a new instance of the class PersonWithGetProperty and map the response to this new object using the class constructor. But how do I achieve this in the getPersonWithGetProperty method?

import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import {Observable} from 'rxjs/Rx';

export class PersonWithGetProperty {
  constructor(public firstName: string, public lastName: string){}

  get fullName(): string {
    return this.firstName + ' ' + this.lastName;
  }
}

@Injectable()
export class PersonService {
    constructor(private http: Http) {
    }

    getPersonWithGetProperty(): Observable<PersonWithGetProperty> {
        return this.http.get('data/person.json')
         .map((response: Response) => <PersonWithGetProperty>(response.json()));
    }
}

Answer №1

You may encounter issues because you are trying to force the parsed json data to act like a class.

When using <PersonWithGetProperty>, you are not actually creating a new instance of PersonWithGetProperty; instead, you are simply informing the compiler to ignore any warnings as you manipulate the data. To create an actual instance of PersonWithGetProperty, you should use the new keyword for construction.

You are almost there already - just include another map function after parsing the output:

@Injectable()
export class PersonService {
    constructor(private http: Http) {
    }

    getPersonWithGetProperty(): Observable<PersonWithGetProperty> {
        return this.http.get('data/person.json')
         .map((response: Response) => response.json())
         .map(({firstName, lastName}) => new PersonWithGetProperty(firstName, lastName));
    }
}

Edit

In order for this solution to work, ensure that you are utilizing RxJS 5:

import 'rxjs/add/operator/map'

For better compatibility in the future, consider employing the pipe syntax which was introduced in later versions of RxJS 5:

// Either
import {map} from 'rxjs/operators'

return this.http.get('data/person.json').pipe(
  map((response: Response) => response.json()),
  map(({firstName, lastName}) => new PersonWithGetProperty(firstName, lastName))
);

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

Properties are determined by both the type and sub-type

A challenging TypeScript challenge. Exploring Multiple Discriminated Union Types This task involves intersecting multiple discriminated union types together, where there exists a relationship between "types" and their corresponding "sub-types." The Main Q ...

Utilizing the <slot> feature in Angular 5 for increased functionality

Currently, I am working on a single page application (SPA) where Vue framework has been utilized for development purposes. Front-End: Vue Back-End: NodeJs Within my application, there are other sub-modules built in Angular 4. I am looking to replicate th ...

Unable to locate the reference to 'Handlebars' in the code

I am currently attempting to implement handlebars in Typescript, but I encountered an error. /// <reference path="../../../jquery.d.ts" /> /// <reference path="../../../require.d.ts" /> My issue lies in referencing the handlebars definition f ...

Bringing Typescript functions into the current module's scope

Is it possible to import and reference a module (a collection of functions) in typescript without the need for the Module. prefix? For instance: import * as Operations from './Operations'; Can I access Operations.example() simply as example()? ...

What is the correct way to specify the type in my functional component?

I was trying to define the type in my component in a different way, I know it can be done using classes but is there a way to achieve this with functional components without exporting the interface? Despite my extensive research, I couldn't find any r ...

Utilizing personalized Angular component as a feature within OpenLayer map

I am exploring the possibility of integrating my own component as a zoom control for an OpenLayers map. I came across some helpful information here, indicating that it can be achieved by creating specific HTML elements. However, I already have a pre-existi ...

Trouble with V-if not updating after property is modified within an async TypeScript function

There is a scenario where one HTML element becomes hidden after an async call returns from the server, while another HTML element is displayed: <template> <div v-if="!showElementTwo">Element 1</div> <div v-if="show ...

Is it necessary for TypeScript classes that are intended for use by other classes to be explicitly exported and imported?

Is it necessary to explicitly export and import all classes intended for use by other classes? After upgrading my project from Angular 8 to Angular 10, I encountered errors that were not present before. These issues may be attributed to poor design or a m ...

Exploring the method to implement unit testing for a nested if condition using Karma-Jasmine within an Angular environment

I have a function and my unit test coverage is currently at 75%, but I am aiming for 100% coverage. This is the function in question: calculateRatingSummary(): void { if (this.averageRating > 0) { this.avgRatings = Math.trunc(this.averageRat ...

Hello, I am looking for a way to maintain a user's active session when transitioning between different Angular applications without constantly prompting them to login. Can you help me

After logging in with my credentials, the session starts. However, if the user does not have an administrator role, I need to redirect them to another application. The challenge is maintaining the active session without requiring the user to log in again ...

The Angular HttpClient is unable to retrieve the message from the response body

I am in the process of implementing a post request using HttpClient to send data to my getFeedback endpoint and display the response in my application. I have a codeService and a dashboard component. Here is the code snippet for the post method in my code ...

Exploring Blob functionality in TypeScript?

I defined a global Blob object: declare global { interface Blob { prototype: Blob; new (name: string, url: string): Blob; } } It is functioning correctly in this code snippet: export const blobToFile = (blob: Blob) => { let file: File | n ...

The property 'x' cannot be found on the data type 'true | Point'

I am dealing with a variable named ctx which can be either of type boolean or Point. Here is how Point is defined: type Point = { x: number y: number } In my React component, I have the following setup: const App = () => { const [ctx, toggleC ...

After upgrading to Angular version 14, ComponentFactoryResolver and AbstractControlDirective have been enhanced with new

After upgrading from Angular version 9 to version 14, I encountered errors related to the ComponentFactoryResolver and AbstractControlDirective. Below is a snippet of my directive.ts file: private componentRef: ComponentRef<FormErrorComponent>; ...

I'm looking for a way to modify the Turkish characters and spaces in the names of JSON data objects. I plan to do this using WebApi

I am facing an issue with fetching data through an API. The JSON data format contains Turkish characters and spaces, causing problems when trying to display the data in a datatable. I have attempted to use the replace and parse functions, but so far, I hav ...

Attempting to retrieve a URL using Angular/Typescript is generating an Unknown Error instead of the expected successful response

I'm trying to make a simple HTTP function call through TypeScript (Angular) to an API hosted as a function in Azure. When I call the URL through Postman or directly in the browser, everything works perfectly. However, when I try to call the URL usin ...

Options for Angular's routerLinkActiveDirective

I have a link that looks like this <li routerLinkActive="active" class="nav-item"> <a [routerLink]="['contracts']" [queryParams]="{ activeOnly: false }" class="nav-link">Contracts</a> </li> As you can see, in the param ...

How can TypeScript generics be used to create multiple indexes?

Here is an interface snippet: interface A { a1: { a11: string }; a2: { a21: string }; a3: { a31: string }; } I am looking to create a generic type object with indexing based on selected fields from interface A. Here is the pseudo-code exampl ...

React Typescript Context state isn't refreshing properly

Struggling to modify my context state, I feel like I'm overlooking something as I've worked with context in the past. The challenge lies in changing the 'isOpen' property within the context. You can view my code here: CodeSand **app.ts ...

Angular ngModel not updating both directions in data binding

<input matInput placeholder="username" [(ngModel)]="userId"> <input matInput placeholder="name" [(ngModel)]="name"> I have made sure to import the FormsModule in my Angular project. import { FormsModule ...