Having issues with Angular resolver not working as expected?

This demonstration showcases the issue:

https://stackblitz.com/edit/angular-routing-with-resolver-observable?file=src%2Fapp%2Fpost-resolver.service.ts

Replacing this.observeLoading$ with of(false) resolves the problem, indicating that it may be related to the fact that this.observeLoading$ receives notifications from a ReplaySubject that can provide multiple notifications...

Below are more specific details:

Firstly, this section resolves successfully:


    async resolve(
        route: ActivatedRouteSnapshot) {
        const topic = await of(false).pipe(
            switchMap((v) =>{
                console.log("MADE IT THIS FAR: ", v)
                return of(new Topic)
            })).toPromise()
        console.log("THE TOPIC RETURNED IS", topic)

        return topic;

In the above example, all logs are printed and navigation is completed.

In the following situation, it prints "MADE IT THIS FAR: false", but then stops:

    async resolve(
        route: ActivatedRouteSnapshot) {
        const id = route.paramMap.get('id')
        const topic = await this.cs.loadingTopicStore$.pipe(
            switchMap((v) =>{
                console.log("MADE IT THIS FAR: ", v)
                return of(new Topic)
            })).toPromise()
        console.log("THE TOPIC RETURNED IS", topic)

        return topic;

Both scenarios appear to be similar. The only distinction lies in using of(false) in the first case and using this.cs.loadingTopicStore$ in the second case, with both cases logging "MADE IT THIS FAR:".

Any insights?

Reported an Issue to Angular

https://github.com/angular/angular/issues/34535

Answer №1

In order to resolve the issue, either complete the stream or restrict notifications to a single false value by utilizing takeWhile in the following manner:

        const topic = await this.cs.loadingTopicStore$.pipe(
            takeWhile(v => v != false, true),
            switchMap((v) => { ...

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 sendFile causing an error due to an invalid JSON format?

Whenever I try to send a file to a client for download, I encounter an exception saying that the JSON is invalid. Is there a better way to send the file, perhaps using res.download and setting the content as JSON? I want to avoid using AngularJS FileSaver ...

Identifying a shift in data model within a component

It seems like there's a piece I might be overlooking, but here's my current situation - I have data that is being linked to the ngModel input of a component, like so: Typescript: SomeData = { SomeValue: 'bar' } Snippet from the vie ...

Error in VueJS/Typescript: Module 'my-module' or its type declarations are not found

Hey fellow developers! I'm currently working on a Vue2 setup with Nuxt and Typescript. I'm facing an issue while trying to install the awesome vue-slick-carousel module via yarn. When attempting to import the module in my component, Typescript th ...

Issue with button click functionality within Angular grid's Celltemplate

Has anyone experienced issues with the click event not working when using celltemplate in AngularJS? $scope.format = function(val){ return val.replace(/\//g, ""); }; var executionColumns = { data: [], ...

Exploring the dynamic duo of Github and vue.js

As I am new to programming and currently learning JavaScript and Vue.js, I have been trying to deploy my first Vue.js app without success. Despite spending hours on it, I still cannot figure it out and need to seek assistance. Every time I try to deploy, ...

Showing a div with 2 unique data attributes - such as displaying Currency and Quantity

My E-Commerce website is created using HTML, JavaScript, and PHP. On the product details page, users can add products to their cart, so I show the total cart value. I need the total amount displayed in a decimal number format (10,2). Currently, when a u ...

The AJAX POST request encountered an error

I apologize for the lackluster title. Basically, when the script is executed, it triggers an 'error' alert as shown in the jQuery code below. I suspect that the issue lies in the structure of my JSON data, but I'm uncertain about the necess ...

Utilizing a Json.Net object within an Ajax request

Is there a way to pass a .Net object in an Ajax call using the Json.Net javascript library instead of json2.js? You can find more information on passing complex types via Ajax calls at this link: I am familiar with how to serialize and deserialize object ...

What is the best way to utilize an "as" property in TypeScript when forwarding props to a component?

I am currently working on creating a collection of components, and I require some of them to have the option of a customizable tag name. For instance, there are times when what seems like a <button> is actually an <a>. Therefore, I would like t ...

Tips for displaying website preloader just once

I recently added a preloader to my website, but I'm having trouble getting it to only play once per visit. Ideally, I want the animation to show up when the site is opened in a new tab or browser window, but not when navigating through the domain by c ...

Saving the API response in Angular 6 as an .html file

I'm currently developing an application that utilizes Angular 6 on the frontend and Laravel 5.8 on the backend. One of my APIs returns a response in HTML format that needs to be saved as an HTML file and opened in a browser. How can I achieve this in ...

What are the steps for combining AngularJS with Java JAAS authentication system?

My web application combines AngularJS on the frontend with Java on the backend. Angular communicates with the Java backend through Restful webservices exchanging JSON data over HTTP. I am in need of developing an authentication mechanism for this app and I ...

The tab title will be invisible if it is located within the second Angular component

Currently, I am utilizing the material css framework in combination with Angular (both latest versions). Everything works perfectly fine when I create tabs within one component. However, when I decide to make each tab a separate component, an issue arises ...

Implementing the adding functionality within an ejs file

I'm currently working on a project that involves fetching data from an API using a simple JavaScript file upon clicking a button. Initially, everything was functioning properly when both the HTML file and JS file were in the same folder, and I could a ...

Trouble with the JavaScript split function

I am trying to split the data in a specific way. Given a string value like this var str = "[:en]tomato[:ml]തക്കാളി[:]"; I am aiming for the following output: tomato,തക്കാളി. I attempted to achieve this using the code sni ...

Is there a way for me to identify the value within useCallback without any intermediaries?

How can I ensure that console.log('success') is triggered when the ids of myFood[myFood.length - 1]?.id and viewableItems[viewableItems.length - 1]?.item?.id match? Despite both values being equal, there seems to be an issue preventing console.l ...

The use of BaseJQueryEventObject and JQueryEventObject is no longer recommended. What is the alternative to replace them?

Transitioning from jQuery v2 to jQuery v3, the interfaces such as BaseJQueryEventObject in type definitions are now deprecated. What should be used as a replacement in the code? For instance, when working with angularjs, functions may look like this: pu ...

Get Subject Alternative Name from X.509 in a Node.js environment

Having trouble retrieving the SAN from a DoD Smart Card, as the subject alternative name is returning othername:<unsupported>. I have not been able to find many examples on how to parse this information. I would have thought that X509 li in node woul ...

Workbox automatically caches the lazy loaded bundles of Angular for improved performance

As I work on my Angular application and integrate Workbox Service Worker(SW) for PWA compatibility, I encounter an issue with my lazy-loaded bundles. While everything functions correctly in development mode due to SW being enabled only in production, the p ...

What steps should I take to address the issue of chart_js.Chart.register in my implementation of chart.js

I am in the process of building a user dashboard application with Next.js. I am fetching data from my node and MongoDB server to populate the dashboard. To display graphs, I am using chart.js. However, when I try to run the code, I encounter an error: Ty ...