The data type 'Observable<any>' cannot be assigned to the type 'StoresSummaryResults'. The property 'Data' is not present in the 'Observable<any>' type

As a newcomer to using the Observable with Angular 2, I am facing an issue where my structure is not receiving the results despite being able to validate the response from my REST API.

Below is the data class in Typescript that I have:

import { RESTResult } from '../common/RESTResult';        // Common Response Structure
import { StoresSummaryData } from './StoresSummaryData';  // Structure of records being returned as an array

export class StoresSummaryResults extends RESTResult {
    Data: Array<StoresSummaryData>;     // Data[] represents an array of StoresSummaryData structure ({ field1, field2 ...)

    constructor() {
        super();
        this.Data = new Array<StoresSummaryData>();    // Ensuring 1 record exists for testing
        this.Data.push(new StoresSummaryData(0, 0));
    }
}

The results are fetched from the REST API.

getResults(): Observable<StoresSummaryResults> {    // Structure 
    return this.http.get(this.RequestHttpPath)
                    .map(this.extractData)
                    .catch(this.handleError);
};

private extractData(res: Response) {
    let body = res.json();
    return body.data || {};
}

StoreInfo: StoresSummaryResults;
public constructor(private storesService: StoresService) { }
showResults(): void {
    this.StoreInfo = this.storesService.getResults();
}

The error message I encounter is:

Typescript Error
Type 'Observable<StoresSummaryResults>' is not assignable to type 'StoresSummaryResults'. Property 'Data' is missing in type 'Observable<StoresSummaryResults>'.

Despite having the Data structure defined, I am unsure about what needs correction.

Answer №1

The property StoreInfo is defined as a StoresSummaryResults, but you are attempting to assign it the output of storesService.getResults() which is of type

Observable<StoresSummaryResults>
.

To resolve this, either update StoreInfo to be of type

Observable<StoresSummaryResults>
, or set it within a subscription:

this.storeSevice.getResults()
    .subscribe(results => this.StoreInfo = results);

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

Stop the direct importing of modules in Angular applications

I have a feature module that declares components and also configures routing through a static function. @NgModule({ declarations: FEATURE_COMPONENTS, entryComponents: FEATURE_COMPONENTS, imports: [ UIRouterModule, ... ] }) export class Fea ...

Understanding ng-app Directive in AngularJS

Kindly take a look at the code snippets I've included below. Scenario 1: Example of an AngularJS Application <div ng-app = ""> <input type="text" autofocus="autofocus" placeholder="Enter your name" ng-mode ...

Error: The class you are attempting to access is

Currently, I am utilizing Vite.js along with TypeScript. My TypeScript file is located in 'src/vmodel/VGraph'. Within the index.html file, I import the aforementioned file as follows: <script type="module" src="/src/vmodel/VGrap ...

Troubleshooting Problem with Angular JS Ng-Repeat

I have a specific situation where I want to showcase the elements that exist in only one array. If an element is also present in another array, there is no need to display it. My HTML structure looks like this: <div ng-repeat="array1Value in array1"&g ...

Sharing AngularJs controllers between different modules can help streamline your

I'm facing an issue with trying to access an array from one controller in another controller. Despite simplifying the code for clarity, I still can't seem to make it work. Here is my first controller: app.controller('mycont1', [' ...

Leverage the power of Angular and Node.js to dynamically generate and configure a new subdomain on an

Currently, I am tackling a project that involves generating sub domains dynamically based on the prefixes entered by users on my website. While everything is functioning properly on my localhost using diet.js and express-subdomain, errors are being encount ...

Angular.js Issue: Repeating elements must be unique - Index tracking not functioning properly

I have been following a tutorial on the Ionic Framework, which utilizes Angular JS to develop a basic Todo application. The app adds a new task by utilizing the .push() method to append a new task object to an array of task objects. An issue arises when a ...

Angular with Firebase: How to ignore a field in a query

I am curious to see if my current structure is compatible with Firebase, or if I need to make adjustments. Let's take a look at an example using the "/rooms" endpoint, which contains an array of Room objects: export class Room { id: number; p ...

React Redux Saga doesn't trigger any actions

Currently, I am attempting to incorporate the following functionality: Users can successfully log in, but precisely after 5 seconds have passed, they are automatically logged out. My approach involves working with JSONWEBTOKEN. Here is my implementation u ...

Is a donut chart graph visible on the webpage?

After successfully creating a bar chart, I decided to work on a donut chart using Angular and d3.js. However, despite creating the donut chart, I'm facing an issue with displaying it on the screen. The DOM shows that it is present, but for some reason ...

Angular does not always interpret the value returned from a Promise.all call

One of the challenges I'm facing is related to the structure of my controller: controller.things = ['a', 'b', 'c']; controller.loading = true; controller.responses = []; controller.handlePromises = function(){ var pr ...

How can I extract a specific data value from a JSON file in Ionic 2?

Here is the JSON data: [ { "id": 1, "label": "saw", "total": "100" }, { "id": 2, "label": "saw1", "total": "300" }, { "id": 3, "label": "saw2", "total": "400" } ] Below is my Typescript code snippet: this. ...

Avoiding multiple HTTP requests on various subscribers in RXJS/Angular

I am currently utilizing the "mainData" service, which is composed of 3 key parts: currentPage is utilized by the paginator component for page navigation and can be updated dynamically. folders holds all folders within the current directory. This observa ...

Execute AngularJS code when a button is clickeddoubleValue

I am in the process of developing a dynamic page where users can add anywhere from 1 to n products effortlessly. Although I have been exploring AngularJS for several months, I find myself facing some challenges when conceptualizing this scenario. The HTM ...

Error: The TypeScript aliases defined in tsconfig.json cannot be located

Having trouble finding the user-defined paths in tsconfig.json – TypeScript keeps throwing errors... Tried resetting the entire project, using default ts configs, double-checked all settings, and made sure everything was up-to-date. But still no luck. H ...

"Freezing issue with Angular big table causing web page to lock up during

Looking for a way to improve the performance of an HTML table search function that is currently running very slowly and freezing the web page upon loading and searching. The table needs to be within the HTML file itself, not pulling data from a server. Any ...

Leveraging Angular with socket.io

While Angular does offer fantastic two way data binding on the client side, I am seeking to expand beyond that! I'm in search of the most optimal method to integrate angular with my server side (specifically SailsJS which incorporates socket.io). Ap ...

Encountering an Uncaught Error in Angular 1.2.9 despite the inclusion of ngRoute module

Even after ensuring the ngRoute file is included and ngRoute is added to dependencyInjection, I am still encountering the following error: Error: [$injector:modulerr] http://errors.angularjs.org/1.2.7/$injector/modulerr?p0=demoModule&p1=Error….org% ...

Struggling to retrieve posted data using Angular with asp.net

I have encountered an issue while sending a post request from Angular to my ASP.NET server. I am trying to access the values of my custom model class (SchoolModel) and I can see that all the values are correct inside Angular. However, when I attempt to ret ...

Challenges with Typescript arise when modifying dependencies within a Firebase function, leading to compilation

Recently, I decided to update the dependencies of my Firebase functions project in order to utilize newer versions of firebase-functions and firebase-admin. However, this led to a requirement for more recent versions of TypeScript and tslint. After making ...