Is there a way to ensure that the observer.next(something) received the value before executing observer.complete()?

I have been working on an Angular app where I am using a resolver to preload data in the component. Below is the code for the resolver:

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): void {
return Observable.create(observer => {
  const searchProductsRequest = this.journeyDataService.searchProductsRequest;
    this.basketService.reset();
    this.progressService.subscribe(
    this.productsPublicApiService.getRecommendedProductCategoriesUsingGET(searchProductsRequest.vrn,
        searchProductsRequest.postcode),
      (response) => {
        // saving work type list
        observer.next(response.categories);
      },
      // handling server errors
      (err) => console.error(err)
    );
    observer.complete();
  });
}

In the component, I have the following code:

ngOnInit() {
   this.productCategoryList = this.route.snapshot.data.productCategoryList;
   console.log('resolve', this.productCategoryList);
 }

However, when checking the value of 'this.productCategoryList', it is null. This is because the response from the server arrives after the complete function is called, resulting in the data not being available for display.

Answer №1

If you want to ensure the "next" handler is fully executed, include the complete call within your code snippet:

(response) => {
        // Save work type list
        observer.next(response.categories);
        observer.complete();
      },

There may be a more standard approach to solving your issue, but a clear understanding of functions like

getRecommendedProductCategoriesUsingGET
and progressService.subscribe is necessary for proper implementation.

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

Is it advisable for a component to handle the states of its sub-components within the ngrx/store framework?

I am currently grappling with the best strategy for managing state in my application. Specifically, whether it makes sense for the parent component to handle the state for two subcomponents. For instance: <div> <subcomponent-one> *ngIf=&qu ...

Why isn't Nodemon monitoring the directory in webpack-typescript-node.js?

Here are the contents of the package.json file for a TypeScript project using webpack and node.js: "scripts": { "build": "webpack", "dev:start": "nodemon --watch src --exec \"node -r dotenv/co ...

Troubleshooting the issue with mocking the useTranslation function for i18n in JEST

Currently, I am facing an issue with my react component that utilizes translations from i18next. Despite trying to create tests for it using JEST, nothing seems to be getting translated. I attempted to mock the useTranslation function as shown below: cons ...

Reactjs may have an undefined value for Object

I have already searched for the solution to this question on stackoverflow, but I am still confused. I tried using the same answer they provided but I am still getting an error. Can someone please help me resolve this issue? Thank you. const typeValue = [ ...

Typescript: Utilizing method overloading techniques

I'm in the process of implementing function overloads that look like this: public groupBy(e: Expression<any>): T { e = this.convert(e, Role.GROUP_BY); this.metadata.addGroupBy(e); return this.self; } public grou ...

The link function fails to execute

I have created a custom Directive. The issue I am facing is that the html template is not being rendered. Upon debugging, I noticed that the link function is never called because the instance function is also never called. To troubleshoot, I added "debu ...

Lazy loading a child component in Angular 8: A step-by-step guide

In my project, I have a complex component that consists of multiple modals (NgbModal). These modals are connected to various child components. My goal is to implement lazy loading for these child components. Dashboard Module | |--> Dashboa ...

Assistance for Angular 2 Style Guide within Atom: Feature Needed!

My manager uses Atom with a set of eight plugins specifically designed for Angular development. I have the same plugins installed on my system, but I seem to be missing a feature that he has. We're unsure which plugin or preference setting is required ...

Differentiating navigation design in various views of Angular 4

I am utilizing a shared navigation with content that changes via RouterModule. Is there a way to modify the design of the navigation in different views? <app-navigation></app-navigation> <router-outlet></router-outlet> For example ...

Creating assets from typescript plugins in Angular 6: A comprehensive guide

Situation I am currently in the process of migrating from Angular 4 and Angular Seed to Angular 6 and Angular CLI. Challenge One issue I am facing is with dynamic loading of plugins within a component using SystemJS. SystemJS.import("client/plugins/" + ...

Triggering blur event manually in Ionic 3

Is there a way to manually trigger the blur event on an ion-input element? The ideal scenario would be with an ionic-native method, but any javascript-based workaround will suffice. My current configuration: Ionic: ionic (Ionic CLI) : 4.0.1 (/User ...

Schedule - the information list is not visible on the calendar

I am trying to create a timeline that loads all data and events from a datasource. I have been using a dev extreme component for this purpose, but unfortunately, the events are not displaying on the calendar. Can anyone offer any insights into what I might ...

Issue with loading CSS in Angular 8 upon refreshing the page after building in production

Here is the structure of my index.html: <!doctype html> <html lang="hu"> <head> <meta charset="utf-8"> <title>WebsiteName</title> <base href="/"> <meta name="viewport& ...

Clearing error messages from a form using the reset button or after cancelling the form

I am having trouble removing the error outline around the input box and error messages displayed below it. When I cancel the form or click on the reset button, the input fields' content along with the error messages should be cleared. However, current ...

How come the information I receive when I subscribe always seems to mysteriously disappear afterwards?

I've been working on a web project using Angular, and I've run into an issue with my code that's been causing problems for a while now. The problem lies in fetching data from a server that contains translations: getTranslations(): Observab ...

The module "@clerk/nextjs/server" does not contain a member with the name "clerkMiddleware" available for export

Currently, I am working on Nextjs 14 with typescript and implementing authentication and authorization using Clerk. Following the instructions in the Clerk documentation, I included the code snippet below in my middleware.ts file: import { authMiddleware } ...

How can I configure React Router V6 to include multiple :id parameters in a Route path, with some being optional?

Currently, I am utilizing react-router@6 and have a Route that was previously used in V5. The route is for vehicles and always requires one parameter (:id = vehicle id), but it also has an optional second parameter (:date = string in DD-MM-YYYY format): &l ...

"Integrating FullCalendar with Cloud Firestore for seamless data management

Is It Possible to Integrate Observable with FullCalendar? I have a collection in Cloud Firestore and I want to display the data from this collection in real-time on FullCalendar. Although I know that using an Observable is necessary for this task, it see ...

Update the Angular material table with the set filtered results

Currently, I have a functioning Angular Material table with search capabilities. However, I am encountering an issue. The problem lies in the fact that when I navigate from 'route A' to 'route B' and pass a value to the search box in t ...

Error while conducting unit testing: Element 'X' is unrecognized

While running the command npm run test, I encountered a specific error in my terminal: 1. If 'app-general-screen' is an Angular component, then verify that it is a part of an @NgModule where this component is declared. 2. If 'app-general-sc ...