Trouble displaying object in template using Angular's ngrx

Within my Typescript interface, I have declared the following structure:

export interface FolderList {
 ADMIN: [Folder[], string, string];
 SUPERADMIN: [Folder[], string, string];
 USER: [Folder[], string, string];
}

The 'Folder' interface is defined as follows:

export interface Folder {
  idFolder: number;
  folderName: string;
}

My goal is to have an object containing three arrays:

FolderList: {
  ADMIN: [[{idFolder: '1', folderName: 'AdminFolder'}], "2 pages", "number 3"],
  SUPERADMIN: [[{idFolder: '1', folderName: 'SuperadminFolder'}], "2 pages", "number 3"],
  USER: [[{idFolder: '1', folderName: 'UserFolder'}], "2 pages", "number 3"],
}

I have declared an attribute in my component like this:

export class SidebarContainer implements OnInit {
  folder$: Observable<FolderList>;
}

This observable is managed within an NGRX effect in the following manner:

loadFolder$ = createEffect(() =>{
   return this.action$.pipe(
   ofType(SidebarAction.loadFirmSuccess),
   switchMap(action => {
    const foldersList = {};

    action.firms.map(
      firm => {
        firm.roles.map((role: string) => {
          foldersList[role] = this.sidebarService.getFolders(action.userId, firm.id.toString() , role, '0', '3');
        });
      }
    );

    return forkJoin(foldersList).pipe(map(folders => {
      return SidebarAction.loadFolderSuccess({folders});
    }));
  })
);
});

The 'role' key corresponds to the roles ADMIN, SUPERADMIN, and USER.

I then utilize a reducer to update the state accordingly:

export const sideBarReducer = createReducer(
 initialSidebarState,
 on(SidebarActions.loadFolderSuccess, (state, action): SidebarState => {
   return {
     ...state,
     folders: action.folders
   };
 })
);

Currently, I am attempting to display the data from this object within the template:

{{folder$.ADMIN | async }}

However, I encounter an error message stating:

Identifier 'ADMIN' is not defined. 'Observable<FolderList>' does not 
  contain such a member

Interestingly, if I simply render 'folder$' like this:

 {{folder$ | async | json }}

I can indeed see the 'ADMIN' property...

What could be causing this issue?

Answer №1

folder$ represents an Observable<FolderList>, so it will not include properties like ADMIN.

However, when using folder$ | async, the ADMIN property will be present due to the use of the async pipe.

  • The Async pipe subscribes to the folder$ observable and returns the most recently emitted value, resulting in folder$ | async displaying the value of FolderList.

To display the ADMIN property, it must be accessed from the subscribed object of FolderList as shown below.

{{ (folder$ | async)?.ADMIN }}

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

Setting the date of the NgbDatePicker through code

I have implemented two NgbDatePicker components in my project: input( type="text", ngbDatepicker, #d="ngbDatepicker", [readonly]="true", formControlName='startDate', (dateSelect)='setNew ...

Finding the right way to cancel a Firestore stream within a Vue component using the onInvalidate callback

Currently, I am utilizing Vue 3 to develop a Firebase composable that is responsible for subscribing to an onSnapshot() stream. I have been attempting to unsubscribe from this stream by invoking the returned unsubscribe() function within both watchEffect ...

What is the most effective way to send messages from Typescript to C#?

Could someone provide guidance on how to properly send a message from Typescript to C#? I have been attempting to receive the message in C# using WebView_WebMessageReceived with the code snippet below: private void WebView_WebMessageReceived(object sender, ...

What is the solution for resolving the Angular error 'Cannot read property "_co.x" of undefined'?

Currently, I am facing an issue with nested JSON objects on a form that sends data to a database. Despite trying the *ngIf statement as recommended in Angular 5 - Stop errors from undefined object before loading data, the view does not update even after th ...

Tips for executing multiple asynchronous calls simultaneously within a nested map function

I am facing a scenario where I have an object with nested arrays of objects which in turn contain another set of nested arrays. To validate these structures, I have an asynchronous function that needs to be executed concurrently while awaiting the results ...

How can I iterate over a specific range in Angular (version 12 and higher) instead of looping through the entire array?

I need help with working with an array called bars stored in a foo property. In my HTML template, I am able to loop through the array like this: <div *ngFor="let bar of foo.bars"> <!-- do something with bar --> </div> Howeve ...

The cache-control header is not being included in the response header in the express app

I am facing an issue where the Cache-Control header is not getting added to the document request in my express / node server. My code successfully adds other headers, but for some reason, it fails when trying to add Cache-Control and I'm unsure why. ...

Managing clearing values/strings for different input types like text/password upon form submission in Angular2

Here is a code snippet for an HTML form: <div> <form class="form-horizontal" role="form" (ngSubmit)="createUser(Username.value, Password.value)"> <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6"> <input type="text" class=" ...

The functionality of `import { Dialogs } from "@nativescript/core"; seems to be malfunctioning

For my project, I am in need of using Dialogs. Unfortunately, the import from @nativescript/core as mentioned in their documentation is not working. I keep encountering this error: Module '"@nativescript/core"' has no exported member &a ...

Error: setPosition function only accepts values of type LatLng or LatLngLiteral. The property 'lat' must be a numerical value in agm-core agm-overlay

Currently, I am utilizing Angular Maps powered by Google @agm-core and agm-overlay to implement custom markers. However, when using the (boundsChange) function on the agm-map component, an error occurs with the message "invalidValueError: setPosition: not ...

Error: BrowserModule has already been loaded

After updating my application to RC6, I encountered a persistent error message: zone.js:484 Unhandled Promise rejection: BrowserModule has already been loaded. If you need access to common directives like NgIf and NgFor from a lazily loaded module.. ...

Discover the location of items within an array

Currently, I am working with a JSON object that has the following structure. My objective is to determine the index based on ID in order to retrieve the associated value. The indexOf function appears to be suitable for arrays containing single values, but ...

Transform the header elements into clickable links

How can I prevent the header items from acting as links when I right-click on them? Here is my code: (I don't want them to open in another window when right-clicked.) [![enter image description here][1]][1] ...

Tips for eliminating the draggable item's shadow in Angular

Is there a way to remove the shadow seen under the backdrop when dragging an item in the Bootstrap modal dialog? In the image provided, I am trying to drag the "Personal Details" button..https://i.stack.imgur.com/uSNWD.png ...

Creating a velocity gauge style graph in Angular 4: A step-by-step guide

Hello, I'm currently working on building a speedtest-inspired application. While everything else is going smoothly, I'm struggling to incorporate a speedometer-like chart in Angular 2/4. Despite searching extensively, I've only come across J ...

Leveraging .tsx components within nested .tsx components in React Native

Currently, I am delving into the world of building apps using TypeScript in React Native. Coming from a background as a Swift developer, adjusting to JavaScript and TypeScript has been an interesting journey. An observation that stood out to me is the cha ...

The 'data-intro' property cannot be bound to the button element as it is not recognized as a valid property

I've been using the intro.js library in Angular 8 and so far everything has been working smoothly. However, I've hit a roadblock on this particular step. I'm struggling to bind a value in the data-intro attribute of this button tag. The text ...

Issues with Angular's *ngIf directive not functioning correctly

Within my template, I have a block of HTML that controls the visibility of a div. <div *ngIf="csvVisible"> <p>Paragraph works</p> </div> This particular code snippet belongs to my component. export class SettingsComponent imple ...

Step-by-step guide for integrating Google Closure Library with Angular 10

Is there a way to integrate the Google Closure Library into an Angular 10 project? I attempted to install it using npm install google-closure-library, but encountered an error stating Could not find a declaration file for module 'google-closure-librar ...

Using an alias to call a function defined in a separate module in TypeScript

The following code snippet is from the v4.js file located inside the uuid folder within Angular's node_modules: var rng = require('./lib/rng'); var bytesToUuid = require('./lib/bytesToUuid'); function v4(options, buf, offset) { ...