Before the service call finishes, Akita queries this.selectAll and returns an empty list

Here is the code snippet from my file named widgetquery.ts:

@Injectable({ providedIn: 'root' })
export class WidgetQuery extends QueryEntity<WidgetState, WidgetTO> {
  public Widget$: Observable<WidgetTO> = this.selectActive().filter(k => !!k);
  public WidgetHistorikk$: Observable<WidgetTO[]> = this.selectAll({sortBy: 'WidgetId', sortByOrder: Order.DESC}).filter(k => !!k);

  constructor(protected store: WidgetStore) {
    super(store);
  }
}

In the front end, I am utilizing *ngIf as shown below:

 <ng-container *ngIf="widgetHistorikk$ | async"> ... </ng-container>

The constructor in my component invokes the following function to retrieve widgets via a service call (which is successful...)

  private fetchWidgetHistorikk() {
    this.WidgetService.getWidgetHistorikk().filter(h => !!h).subscribe(liste => {
      this.WidgetStore.add(liste);
    });
  }

However, the selectAll query initially returns an empty list [ ] before returning the actual data [ widget1, widget2 ].

It is undesirable to receive the empty list first, even though it may occasionally be the case that the real data is indeed empty...

I wonder why Akita is providing me with the empty list first?

Is there a way to use a "flag" or an "initialization state" to instruct Akita not to return data from the query until the service call is completed, and data has been added to the store through this line of code: this.WidgetStore.add(liste); ?

Answer №1

For those utilizing entity storage, one approach is to initially set the loading state to true. Once a response is received, this can then be switched to false. Within your query, you can conditionally display a loading user interface or take alternative actions based on the loading state.

storage.activateLoading(true);

Furthermore, leveraging combineLatest allows for data retrieval upon completion of the loading process.

Answer №2

The reason you are receiving an empty list is because Akita always emits the initial value, as mentioned in the documentation. To bypass this, you can use rxjs skip(1) (or filter out empty or undefined values).

If you prefer not to display empty content on the user interface, you can simply do:

<ng-container *ngIf="(widgetHistorikk$ | async)?.length > 0"> ... </ng-container>

Another option would be to hide the content when the Store's loading status is true.

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

Displaying a component inside a different component

I'm attempting to display components inside another component, but even when I try to include div elements within the component, they don't show up. const DisplayComponent = () => { return ( <div> <DisplayContent ...

Error message stating NullInjectorError with NgxSpinnerService; encountered No provider for t while attempting to host on Firebase

As I attempt to deploy my app on Firebase, everything functions properly in localhost. However, upon successful hosting on Firebase at the Firebase domain, an issue arises: NullInjectorError: StaticInjectorError(wo)[class{constructor(t,e) at SpinnerServic ...

Adal TypeScript Document

Recently, I've been experimenting with the TypeScript version of adal.js. As part of my setup process, I'm referring to this link to install adal.ts. However, after executing the command: npm install adal-typescript --save a new "node_modules" ...

The best location for storing Typescript files within an ASP.NET Core project

My Typescript app is built on AngularJS 2 with ASP.NET Core, and currently I store my TS files in the wwwroot directory. While this setup works well during development, I am concerned about how it will function in production. I aim to deploy only minified ...

How can we manually initiate a state change from the server side to the client view in Angular Universal?

We are currently working on an Angular Universal application built with angular/cli version 1.4.3 and node version 6.9.5. Our issue lies in the transition between server-side view and client view. The switch occurs before we have obtained all the necessar ...

Utilize Angular 5 to implement URL routing by clicking a button, while also preserving the querystring parameters within the URL

I have a link that looks like this http://localhost:4200/cr/hearings?UserID=61644&AppID=15&AppGroupID=118&SelectedCaseID=13585783&SelectedRoleID=0 The router module is set up to display content based on the above URL structure { path: & ...

Ways to access the values of checkboxes that are initially checked by default

Recently, I was working on a project that involved multiple checkboxes. My goal was to have the checkboxes pre-checked with values in the form (using reactive form). Users should be able to unselect the boxes as they wish and the data would be stored accor ...

Angular - Display shows previous and current data values

My Angular application has a variable called modelResponse that gets updated with new values and prints them. However, in the HTML, it also displays all of its old values along with the new ones. I used two-way data binding on modelResponse in the HTML [( ...

Currently, I am working on developing a to-do task manager using Angular 2. One of the tasks I am tackling involves updating the value

I'm facing an issue with managing to-do tasks. I would like to update the value of an option in a select dropdown when the (change) event is triggered. There are 2 components: //app.component.ts //array object this.dataArr[this.counter] = {id: this ...

Only the final defined document is instantiated by the Swagger-ui-express module

Currently, I am working on a Typescripted nodejs server and facing an issue with defining different swagger paths for separated controllers. The problem is that the swagger-ui-express module only displays the last defined document in the specific route. I ...

Navigating back to the login page in your Ionic V2 application can be achieved by utilizing the `this.nav

Running into an issue with navigating back to the login screen using Ionic V2. Started with the V2 tabs template but added a custom login page, setting rootPage = LoginPage; in app.components.ts. When the login promise is successful, I used this.nav.setR ...

issue with global variable not functioning properly within a sub-function in Angular 7

I have a question that needs clarification import { Component, OnInit,ViewChild,ElementRef } from '@angular/core'; import {Http,Headers} from "@angular/http"; import { Observable, Subject, asapScheduler, pipe, of, from, interval, merge, ...

You cannot utilize Lesson as a JSX Component in Next JS TypeScript

Below is my updated page.tsx code: import Aspects from '@/components/Aspects'; import FreeForm from '@/components/FreeForm'; import Lesson from '@/components/Lesson'; import React from 'react'; import { Route, Route ...

Tips for generating a fixed-length array from multiple arrays with different lengths, focusing on selecting items from each array according to their significance

In order to create a quiz, I am looking to extract 'questions' from various 'topic' arrays. These topics are selected based on the user's preference and are used to populate a question bank for a 20-question quiz. The topics rated ...

Injectable error occurred while injecting one @Injectable() into another

I'm encountering an issue with Angular2 Dependency Injection. When attempting to inject one class into another, I am receiving the following error: Error Message: "Cannot resolve all parameters for 'ProductService'(undefined). Make sure tha ...

Need to import Vue component TWICE

My question is simple: why do I need to import the components twice in the code below for it to function properly? In my current restricted environment, I am unable to utilize Webpack, .vue Single File Components, or npm. Despite these limitations, I mana ...

Changing the color of the pre-selected date in the mat-datepicker Angular component is a key feature that can enhance the

Is there a way to adjust the color of the preselected "today" button in mat-datepicker? https://i.stack.imgur.com/wQ7kO.png ...

Supertest and Jest do not allow for sending JSON payloads between requests

Below is the test function I have written: describe("Test to Create a Problem", () => { describe("Create a problem with valid input data", () => { it("Should successfully create a problem", async () => { const ProblemData = { ...

Utilizing ExpressJS in a NodeJS application with ES6 and Typescript

After confirming my information, I discovered that in an ES6 application, it is necessary to import dependencies using import .. from '..' instead of var .. = require('..'). I made the necessary changes to the imports, but encountered ...

Is it possible to incorporate regular React JSX with Material UI, or is it necessary to utilize TypeScript in this scenario?

I'm curious, does Material UI specifically require TypeScript or can we use React JSX code instead? I've been searching for an answer to this question without any luck, so I figured I'd ask here. ...