Cannot find the specified attributes in the 'Observable<PizzaState>' type

I am attempting to create pizzas that I receive from the store, but I am encountering a red flag on this issue.

this.pizzas$

I'm unsure why this is happening, but when I remove : Observable<PizzaState>, it resolves the problem. However, I want to maintain type safety. How can I resolve this?

Here is the full code:

import { Store } from '@ngrx/store';
import { ProductesState } from '../shared/models/productesState.model';
import { Observable } from 'rxjs';
import { PizzaState } from '../shared/models/pizzaState.model';

@Component({
  selector: 'app-read',
  templateUrl: './read.component.html',
  styleUrls: ['./read.component.css']
})
export class ReadComponent implements OnInit {

  public pizzas$: Observable<PizzaState>;

  constructor(private store: Store<ProductesState>) { }

  ngOnInit() {

    this.store.select('pizzas').subscribe(store => {
      this.pizzas$ = store;
    });

  }

}

This is my reducer:

import { PizzaState } from 'src/app/shared/models/pizzaState.model';
import * as fromPizzas from '../actions/pizzas.action'

export const initialState: PizzaState = {
    data: [],
    loaded: false,
    loading: false
}

export function reducer
    (state: PizzaState = initialState, action: fromPizzas.PizzasAction): PizzaState {

    switch (action.type) {

        case fromPizzas.LOAD_PIZZAS: {
            return {
                ...state,
                loading: true
            }
        }

        case fromPizzas.LOAD_PIZZAS_SUCCESS: {
            return {
                ...state,
                loaded: true,
                loading: false
            }
        }

        case fromPizzas.LOAD_PIZZAS_FAIL: {
            return {
                ...state,
                loaded: false,
                loading: false
            }
        }

        default: {
            return state;
        }

    }

}

And here is the configuration inside node module:

StoreModule.forRoot(
      { 'pizzas': reducers.pizzas }
    )

Thank you for your assistance :-)

Answer №1

If you want the data to remain as an Observable, it's advisable not to subscribe to it right away, as subscription will extract it from the Observable. You have two alternatives:


public pizzas$: PizzaState;
...
this.store.select('pizzas').subscribe(store => {
  this.pizzas$ = store;
});

or


public pizzas$: Observable<PizzaState>;
...
this.pizzas$ = this.store.select('pizzas');

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

React type-script does not trigger the onClick event for CheckBox

I have created a custom component called MyCheckBox (which I am using as a helper component). I imported this component into another one, but for some reason, the event is not being triggered when I try to click on it. Here is the code for reference: MyC ...

Encountering a type error while dispatching actions in a React component using TypeScript

When I submit the form, I encounter a type error dispatch(createNote(data)) in handleSubmit. However, if I dismiss the error and proceed with submitting the data, it gets saved successfully in indexeddb. Error: **Argument of type '(dispatch: Dispatch ...

Fixing the error "cannot call a function which is possibly undefined" in React and TypeScript

Seeking a resolution to the error "cannot invoke an object which can possibly be undefined" by utilizing react and typescript. What is the issue at hand? The problem arises when using the useContext react hook to create a variable (dialogContext) in compo ...

Issue with running tests on Angular Material components causing errors

Running ng test on my Angular 8 project with Angular material components is resulting in multiple failures. The issue seems to be related to missing test cases for these scenarios. DeleteBotModalComponent > should create Failed: Template parse errors: & ...

Filtering FieldSelector options in react-querybuilder: A step-by-step guide

I am currently working on a task to eliminate the fields from FieldSelector that have already been utilized. In my custom FieldSelector component, let's assume there are fields A, B, C, D, E available. If A & B have been used, they should not be ...

Tips for removing the splash screen in Ionic 2

I've made changes to the config.xml file by setting the value of the splash screen to none. As a result, the splash screen no longer appears. However, I'm now encountering a white screen instead. Is there a way to prevent this white screen from a ...

What could be causing Angular's Renderer2 to generate inaccurate outcomes?

Having an array of queries with varying positions of underscores: Newtons __________ Law dictates __________ is the result of an object's mass and speed. My goal is to show each question to users, replacing the blanks with in ...

The value from select2 dropdown does not get populated in my article in Angular

I am attempting to link the selected value in a dropdown menu to an article, with a property that matches the type of the dropdown's data source. However, despite logging my article object, the property intended to hold the selected dropdown value app ...

Looking to substitute the <mark> element within a string with the text enclosed in the tag using JavaScript

In need of help with replacing tags inside a string using JavaScript. I want to remove the start and end tags, while keeping the content intact. For example, if my input string is: <div class="active"><mark class="active-search-position">The ...

What's the most effective way to constrain focus within a Modal Component?

Currently working on a library of components in Angular 8, one of the components being a modal that displays a window dialog. The goal is to prevent focus from moving outside the modal so that users can only focus on the buttons inside by using the Tab but ...

Prevent ASP.NET Core routing from intercepting Angular 5 routing calls during deep linking operations

I am currently utilizing ASP.NET Core MVC for managing API calls, with routing set to api/*. Additionally, Angular 5.x is being used alongside its own routing system. Everything functions smoothly when running locally (Core on port 5000 and Angular on 420 ...

When applying css font-weight bold with jsPDF, spaces may be removed from the text

Whenever I apply the font-weight bold in jspdf, it seems to cause the text to lose its spacing. You can compare the before and after images below which were extracted from the pdf file generated: https://i.stack.imgur.com/0BPWP.png Below is the code snipp ...

Getting an error message with npm and Typescript that says: "Import statement cannot be used outside

After developing and publishing a package to npm, the code snippet below represents how it starts: import * as aws from "@pulumi/aws"; import * as pulumi from "@pulumi/pulumi"; export interface ... export class controlplaneDependencies ...

Error Encountered with Visual Studio Community Edition 2019 and AspNetCore 3.1 Angular Template

When trying to create an AspNetCore 3.1 Angular template with Authorisation for individual accounts in Visual Studio Community Edition 2019, I encountered an error in the error list: The error message states that Package AutoMapper 9.0.0 is not compatible ...

The error thrown is: "TypeError: device.devices.map is not a valid function

I encountered an error after adding products to the page and I'm having trouble identifying the cause. const {device} = useContext(Context) 91 | </div> > 92 | <div className="inner-display-collection"> | ^ ...

When employing the pipe function within *ngFor, the webpage's refresh may vary, causing occasional updates

Utilizing angular2-meteor, I have already implemented pure: false. However, the pipe seems to be running inconsistently. For more details on the issue, please refer to my comments within the code. Thank you. <div *ngFor="#user of (users|orderByStatus) ...

Attempting to extract solely the animated elements from a GLTF file

Is there a way to extract only the animations from a gltfAsset in order to create an animation file? I'm wondering if there is a library or an easier method to accomplish this task. private async createAnimationAssets(props: Props, animations: Animati ...

Issue with Typescript and React: Property not found on type 'IntrinsicAttributes'

While working on my app using Meteor, React, and Typescript, I encountered a transpiling error: The property 'gameId' is not recognized in the type 'IntrinsicAttributes & {} & { children?: ReactNode; } In my project, I have a com ...

Absence of a connectivity pop-up within Ionic 2

Whenever the app loads, my "network check" should run automatically instead of waiting for the user to click on the Check Connection button. I tried putting it in a function but couldn't get it to work. Can anyone help me identify the syntax error in ...

Ways to display autocomplete suggestions when an input field is in focus

In my angular6 application using the latest angular-material, I have implemented an auto-complete feature using the component mat-autocomplete with a mat-input. The goal is to display all available auto-complete options when the user focuses on the input ...