Combining and consolidating a unique observable array that originated from the ngrx state tree

Upon making an API call, the data is retrieved and processed through ngrx-effects before being stored in the state tree. From there, I can access the data in my angular component's constructor and utilize rxjs methods like mergeMap and reduce to format the data for UI display.

After successfully replicating the process on stackblitz, everything is functioning as intended. You can view the implementation through the following link:

link: https://stackblitz.com/edit/typescript-rxjs-reduce

Workflow State Tree:

export interface WorkflowsState {
    workflows: workflow[] | [],
    allWorkflowsLoaded: false
}

Overall App State Tree:

interface AppState {
    workflows: WorkflowsState,
    smartNav: SmartNavState
}

Effect Method:

@Effect() loadWorkflows$ = this.actions$.pipe(
    ofType(WorkflowsActionTypes.LoadWorkflows),
    switchMap((action: LoadWorkflows) =>
    this.http.get(url)
    .pipe(map((res: Workflow[]) => new WorkflowsLoaded(res))))
);

Reducer Section:

switch (action.type) {
    case: WorkflowsActionTypes.WorkflowsLoaded:
    return {
        allWorkflowsLoaded: true,
        workflows: action.payload
};

Public Variables Declared in Dashboard Component:

public workflows$: Observable<workflow[]>;
public dashboardTile$: Observable<dashboardTile[]>;

Constructor with Selector:

constructor(private store: Store<AppState>) {
    this.workflows$ = this.store.select(state => state.workflows.workflows);
}

Despite successfully fetching data from the API, I am encountering an issue where the dashboardTile$ variable returns null. I am aiming to achieve the same console output as demonstrated in the provided stackblitz example. Any insights on why this discrepancy may be occurring would be greatly appreciated.

Answer №1

After a long struggle of 2 hours, I finally discovered that the issue was caused by using the reduce operator instead of the scan operator. The ngrx state object was incomplete, leading to an empty accumulated array. Switching to the scan operator resolved the problem and provided the desired output. This particular post on Stack Overflow was instrumental in helping me identify and fix the issue.

Reduce returns empty array, however scan does not

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

NVDA now prioritizes reading only the most recently loaded content in a div, disregarding the rest

When loading 3 different div elements dynamically based on search criteria, I have added attributes "role='alert'", "tabindex='0'", and "aria-live='polite'" to each div. The DOM loads all 3 div el ...

A technique for adding a border to a Mat Card that mimics the outline of a Mat Form Field within a custom

I am faced with a unique design challenge where I am utilizing the MatCardComponent and each card contains a table. I would like to incorporate a floating label within the border gap similar to what is seen in MatFormField. https://i.stack.imgur.com/51LBj ...

Leverage Async Await for Setting Response Data in TypeScript

Currently, I am making multiple API requests with different data and storing all the responses in an array. Then, I am using .map to map the response array to my original array to update the data. However, it seems like the process is not working correctly ...

Determining the Clicked Button in ReactJS

I need help with a simple coding requirement that involves detecting which button is clicked. Below is the code snippet: import React, { useState } from 'react' const App = () => { const data = [ ['Hotel 1A', ['A']], ...

Building TypeScript Model Classes

Greetings! As a newcomer to TypeScript with a background in both C# and JavaScript, I am on a quest to create class models resembling those found in C#. Here's my attempt so far: export class DonutChartModel { dimension: number; innerRadius: ...

Adding a badge to a div in Angular 6: What you need to know!

How can I add a badge to a specific div in Angular 6? I have dynamic div elements in my HTML. I want to increase the counter for a specific div only, rather than increasing it for all divs at once. For example, I have five divs with IDs div1, div2, div3, ...

What is the difference between TypeScript's import/as and import/require syntax?

In my coding project involving TypeScript and Express/Node.js, I've come across different import syntax options. The TypeScript Handbook suggests using import express = require('express');, while the typescript.d.ts file shows import * as ex ...

What are some ways to use SWR for mutating data specific to a certain ID?

I have scoured the internet for answers to no avail. It's baffling because I expected this issue to be quite common. Take, for instance, the scenario where we need to retrieve a post with a specific id: const { data } = useSWR(`/api/post/${id}`, fetc ...

Using react-hook-form to easily update form data

While working on my project with react-hook-form for updating and creating details, I encountered a problem specifically in the update form. The values were not updating properly as expected. The issue seems to be within the file countryupdate.tsx. import ...

"Integrating Orgchart with Typescript in Angular4: A Step-by-Step

mxResources.loadDefaultBundle = false; var bundle = mxResources.getDefaultBundle(RESOURCE_BASE, mxLanguage) || mxResources.getSpecialBundle(RESOURCE_BASE, mxLanguage); // Ensures synchronous requests are handled properly ...

Using template references in ViewChildren

I'm facing an issue trying to utilize ViewChildren to create a QueryList from TemplateRef, but I am unable to pass it to the input component. For instance: Component.ts: @ViewChildren(TemplateRef) cellTemplates: QueryList<TemplateRef<any>& ...

Creating a HMAC-SHA-256 signature in JavaScript for datatrans: A step-by-step guide

I'm currently working on an Angular project that involves implementing the Datatrans payment system. Unfortunately, I have been facing difficulties in generating a sign for the payment. I have been following the process outlined in this link (enter l ...

Encountering the error message "Cannot assign 'Void' to boolean while employing the .find() function"

One particular line of code is causing the issue at hand. const t: GridType = gridDef.find( a => { a.GridName == core.GridStyle; return a; } ); The error message that I am encountering reads as follows ERROR in src/app/grid-builder/builder-scratc ...

Instead of using `await asyncCall()`, try using `(await asyncCall())[0]`

After examining the Typescript code below, I'm curious to understand the rationale behind assigning myArray with (await asyncRPCCall())[0] instead of simply using await asyncRPCCall(). Why is the result placed inside () and why return only the first e ...

"Encountering an issue with mounting components in React Unit Testing with Jest and Typescript

Having developed a simple app with components, here is the code: import GraphicCanvas from './Graphing/GraphCanvas'; import { drawCircle } from './Graphing/DrawCircle'; function App() { return ( <div className="App"&g ...

"Upon submitting a form in React JS, the components will automatically trigger a

Within my application, there is a Mobx storage in conjunction with a modal window component. The form within the modal window allows me to collect all the properties and push them into an array named 'cart' within the storage as an object. Take a ...

What is the best way to create a custom interval gap for polling using RxJS in Angular?

Trying to create a gap/delay between each interval call, but the current method is not working as expected. pollOnInterval(threshold = 4000): Observable<any> { let thresholdValue = threshold; const increaseBy = 2000; const maxCount = 10 ...

Angular's Async Pipe displaying outdated data

I am encountering an issue with my Angular async pipe setup. Here is the code snippet: <ng-container *ngIf="showProjects && (projects | async) as projectList; else loading"> In my projects.page.ts file, I have the following funct ...

Declaring and accessing class variables in Angular 4

I am facing an issue with the following TypeScript model: export class User { email: string; token: string; username: string; bio: string; image: string; constructor() {} } When I attempt to instantiate this model in another TypeScript file, ...

What are the steps to enable generators support in TypeScript 1.6 using Visual Studio Code?

I have been working with ES6 in Visual Studio Code for some time now, but when I attempt to transition to TypeScript, I encounter errors like: Generators are only available when targeting ECMAScript 6 Even though my tsconfig.json file specifies the ES6 ...