Once StoreModule.forFeature(...) has been included, the stored data becomes inaccessible

I am currently working on multiple projects within a single Angular 8 app... Previously, I had @ngrx/store implemented in only one project, but now I have added @ngrx/store to every project. Due to having multiple stores, I now need to import StoreModule.forFeature in every project's app.module

imports: [
    StoreModule.forRoot({}),
    StoreModule.forFeature("project1store", fromStore.reducer1)
]

For the second project, the import looks like this

 imports: [
        StoreModule.forRoot({}),
        StoreModule.forFeature("project2store", fromStore.reducer2)
    ]

The problem lies in the fact that in redux, all data is visible but not accessible. Additionally, I am encountering errors for every reducer (even though they were working fine previously)

store.js:994 @ngrx/store: The feature name "UserProfileReducer" does not exist in the state, therefore createFeatureSelector cannot access it. Be sure it is imported in a loaded module using StoreModule.forRoot('UserProfileReducer', ...) or StoreModule.forFeature('UserProfileReducer', ...). If the default state is intended to be undefined, as is the case with router state, this development-only warning message can be ignored.

I am importing everything using a barrel (index.ts) from fromStore. Each app has its own barrel, and upon rechecking, all paths are correct...

As seen in this image, all data is present, but after StoreModule.forFeature it cannot be retrieved https://i.sstatic.net/epbjl.jpg

EDIT:

I believe the issue lies here https://i.sstatic.net/SbEYc.jpg

Previously, I accessed data like store.siteReducer but now it needs to be retrieved as state.app2.siteReducer

Where and how should I add this app1 or app2?

Thanks

EDIT 2

I can retrieve data directly in a component using this method

 select((state: any) => state.app1.SiteReducer)
.subscribe(res => {...})

However, this requires changes to be made in all app components. Is there a way to implement this directly in the selector? I have attempted the following

 export const getState = createFeatureSelector<States>("app1");

export const getSite = createSelector(
  getState,
  (state: States) => state.app1.siteReducer
);

but it results in an error

Property 'app1' does not exist on type 'States'

Below are my state definitions

export class States { 
data: { site: SiteModel | null; error: string }; 
} 
export const InitialState = {
 data: { site: null, error: null 
} 
};

If I remove the model State from the selector and replace it with any, everything works as expected

export const getState = createFeatureSelector<States>("app1");

    export const getSite = createSelector(
      getState,
      (state: any) => state.app1.siteReducer
    );

I have also tried adding the following to the states model

export class States {
  siteReducer: {data: { site: SiteModel | null; error: string }};
}

export const InitialState = {
  siteReducer: {data: { site: null, error: null }}
};

However, this results in a nested siteReducer object within the store (as shown below)

app1{
    siteReducer{  // get this parent object
        siteReducer:{
            data:{..}
  }}
}

Answer №1

To enable nested level state in your feature selector configuration:

export const selectState = createFeatureSelector<ISomeState>(
    "app2"
);

export const selectSomeState = createSelector(
    selectState,
    state => state.someProp.someProp
);

Next, configure your reducer:

import { ActionReducerMap } from "@ngrx/store";

export interface ISomeState {
    userState: IUserState ; // add your state interface here
}

export const reducers: ActionReducerMap<ISomeState> = {
    userState: userReducer // your reducer
};

Import the reducer into your module:

 StoreModule.forFeature("app2", reducers)

If this solution does not work, please consider sharing your source code on GitHub for further assistance.

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

Adding an external JavaScript library file to a specific component in Angular 7 is a straightforward process. Let's walk through the

As a beginner in Angular framework, I've encountered an issue while working on creating a custom HTML template using Angular version 7. My template consists of various pages like home, about, product, etc. Specifically, on the home page, I am trying t ...

Angular does not display B64 images

I am trying to show a Base64 image string in an Angular application. When I use a static image string, it displays correctly. However, when the image is dynamic, some issues arise. Below is the code from my component.ts file: this.Socket_io = socketIo(&ap ...

substitute one item with a different item

I am facing an issue with updating the address object within an organization object. I receive values from a form that I want to use to update the address object. However, when I try to change the address object in the organization using Object.assign, i ...

Missing expected property in TypeScript casting operation

I recently came across this intriguing playground example outlining a scenario where I attempted to convert an object literal into an object with specific properties, but encountered unexpected results. class X { y: string; get hi(): string { ...

Incorporating the Angular "dist" build file into the Node.js server.js script

Having some trouble deploying my MEAN stack app on Heroku. Managed to commit the app, but struggling to connect the ng build "dist" file to my server.js file. This is the snippet from my server.js file where I'm attempting to link the file: var dist ...

Arranging Pipe Methods in RxJS/Observable for Optimal Functionality

In the class Some, there is a method called run that returns an Observable and contains a pipe within itself. Another pipe is used when executing the run method. import { of } from 'rxjs'; import { map, tap, delay } from 'rxjs/operators&ap ...

Ways to display two tabs in reactjs

My typical app is running smoothly, but I am trying to implement a feature where it launches two tabs upon opening. The first tab should open with a normal path of '/' and the second one with a path of '/board'. Here's an example: ...

Guide to collapsing all menus in an Angular 4 Accordion

I am a beginner with Angular 4 and I have implemented an accordion with the structure of Categories, Groups, and Subgroups. When I click on a category, it displays all the groups within that category. Similarly, when I click on a group, it shows all the s ...

Fetching server-side data for isomorphic React app: A guide to accessing cookies

Currently, I am in the process of developing an isomorphic/universal React + Redux + Express application. The method I use for server-side data fetching is quite standard: I identify the routes that match the URL, call the appropriate data-fetching functio ...

What is the method for adding a close button to nebular tabs?

A key requirement is to display a close button after each tab has been created. Check out the code snippet that needs to be implemented: <nb-route-tabset [tabs]="tabs"></nb-route-tabset> I have thoroughly reviewed their documentati ...

What is the best way to implement debouncing for an editor value that is controlled by the parent component?

Custom Editor Component import Editor from '@monaco-editor/react'; import { useDebounce } from './useDebounce'; import { useEffect, useState } from 'react'; type Props = { code: string; onChange: (code: string) => void ...

Adjust the range slider's color depending on its value

I'm looking to customize the color of a range slider as its value increases, switching from red to green. Below is the code I've tried, but it's not quite working as intended. The goal is for the color to change based on the value of masterR ...

Experiencing code coverage challenges in Angular 8 relating to function properties

https://i.sstatic.net/WQpVB.png Looking for suggestions on how to create a unit test case in Jasmine to address the code coverage problem. Any ideas? ...

Top-notch approach for consolidating Typescript Declaration files into a single comprehensive file

Is there any efficient way to package declaration files together without using the module declaration approach? declare module "path/to/file" { ... } declare module "path/to/file/sub/file" { ... } and so on. I have encountere ...

Utilize ngModel to access input files in the array

Within my form, there is a file upload input: <input type="file" [(ngModel)]="item.image" name="image" #image> Can I retrieve #image.files[0] using the ngModel item.image directly (without creating a reference)? If not, what exactly does ngModel s ...

node-ts displays an error message stating, "Unable to locate the name '__DEV__' (TS2304)."

I recently inserted __DEBUG__ into a TypeScript file within my NodeJS project. Interestingly, in VSCode, no error is displayed. However, upon running the project, I encounter an immediate error: error TS2304: Cannot find name '__DEBUG__'. I att ...

Issue with PrimeNG DataTable contextMenu alignment inside TabView

I encountered an issue with displaying a DataTable - contextMenu when it is placed within a TabView. The contextMenu was appearing off-center. However, the DataTable - contextMenu worked perfectly fine when it was not wrapped in a TabView. To address this ...

The Jasmine Angular PrimeNG test for WindowMaximizeIcon is encountering 16 failures

Currently in the process of upgrading my Angular application from version 15 to 16. Encountered an issue with my Jasmine test that involves PrimeNG's dialog. The error message is shown below: Chrome 114.0.0.0 (Windows 10) IncidentGridComponent should ...

Compiler is unable to comprehend the conditional return type

I've done some searching, but unfortunately haven't been able to find a definitive solution to my issue. Apologies if this has already been asked before, I would appreciate any guidance you can offer. The problem I'm facing involves a store ...

Integrating Arrays and Strings in React's Mapping Function

I am looking to merge the data from rows and rows2. If either of them is empty, I want to set their values to "none". How can I achieve this without creating multiple empty rows? To see the issue in action, check out the Codesandbox HERE <TableBody ...