I possess two distinct components that share a navbar, however, when I select a link, a list is retained in both components but is refreshed within the ngOnInit

I'm currently working on a project that involves Angular and Firebase integration. Among the components in this project are profile, gallery, user pages, and others. However, I have encountered an issue that needs to be resolved.

In the navigation bar of the application, the main menu allows users to navigate between different pages. Of particular importance are:

  1. Profile
  2. Gallery

The Profile component displays publications posted by users. When navigating to the Profile page, the publications by a specific user are loaded using ngFor to iterate through and display them.

The Gallery component showcases all publications stored in Firebase. This page is also the main landing page when users log into the platform. The publications are loaded and displayed using a ngFor loop.

The problem arises after logging in: when transitioning from the Profile page back to the Gallery page, only the publications shown in the Profile are loaded again, instead of all the publications available.

Even with separate services for each component, the issue persists. Any assistance would be greatly appreciated. Below is some example code showcasing the problem:

HTML Code for Gallery Component

<div class="col-lg-9">
    <div id="showResultList" class="row mt-5">

        
        <app-loading-spinner  *ngIf="page.loading | async"></app-loading-spinner>

        <div class="col-xl-4 col-lg-6 col-md-6 col-sm-6" id='megustanlaspapas' loading="lazy" *ngFor="let pub of page.data | async">
            <app-card-publication [pubR]='pub'></app-card-publication>
        </div>
    </div>
    <div id="loadMore" *ngIf='!loadAll' class="row mt-2 mb-3"> 
        <button class="btn btn-outline-info" (click)='loadMore()'>
            <i class="fas fa-search-plus"></i> Load More
        </button>
    </div>
</div>

Gallery Component ngOnInit

constructor(private route: ActivatedRoute, public page : ScrollPaginationPublicationsService) {}

  ngOnInit(): void {
     this.loadAll = false;
     this.page.reset();
     this.page.init('publications', 'date');
  }

Component Used in Gallery This component searches the Firestore Database using various attributes.

... (additional content omitted for brevity)

Thank you for any help or insights into resolving this issue.

View Gallery Image 1

View Profile Image

View Gallery Image 2

UPDATE:

Upon further investigation, I have identified the source of the error. In the Profile service, a search is conducted within the 'publications' collection in Firebase. This query retrieves documents using valueChanges and a pipe function which may be causing the issue.

... (additional content omitted for brevity)

Answer №1

Finally, after extensive troubleshooting, I have managed to resolve the issue at hand. The problem stemmed from AngularFirestore module continuously subscribing to x.valueChanges(), even after the component had been destroyed.

The solution was simple:

Add an ngOnDestroy method to unsubscribe from valueChanges like so:

sub: any;

constructor(private afs: AngularFirestore){}

getRecords() {
    this.sub = this.afs.collection('records')...;
}

ngOnDestroy(){
   this.sub.unsubscribe();
}

With that, the issue is resolved. Hopefully, this solution proves helpful for others facing a similar challenge in the future.

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

There was an error in processing node npm due to Z_DATA_ERROR with error code errno -3, indicating an

When attempting to run my TypeScript project using tsc, I encountered the following error: Found 181 errors in 4 files. Errors Files 1 node_modules/@types/eslint-scope/node_modules/@types/eslint/helpers.d.ts:1 1 node_modules/@types/eslint/hel ...

When the input type is set to 'number', FormGroup.valueChanges will only trigger on 'change' and 'blur' events

Issue: To replicate the problem, visit this Stackblitz link: https://stackblitz.com/edit/angular-feu1az The event handler is behaving unexpectedly when the value of the last field (of type number) is changed. Unlike Text 1 and Text 2 fields where the eve ...

Assistance required in configuring eslint for a monorepo with Yarn 3 and TypeScript

I've been struggling to configure ESlint in my monorepo while using Yarn 3.2.4 as the package manager. To see an example project, check out this GitHub repository. Here is the structure of the project: /monorepo ├── /configs │ ├── /esl ...

Typescript - The type is lacking certain properties from another type, despite having all options defined

I have defined the following TypeScript declarations: type TDisplayKey = "a" | "b" | "c"; const DISPLAY_KEYS: Record<string, TDisplayKey> = { A: "a", B: "b", C: "c" }; const DISPLAY_KEY_TITLES: Record<TDisplayKey, string> = { [DISPLA ...

Angular2 Auth Guard causing empty page display when Observable.of(true) is used

While using auth-guard to protect certain routes, I'm encountering an issue where a blank page is displayed when attempting to retrieve a refresh token. The authenticated() method returns an Observable and the code seems to be functioning correctly wi ...

Implement new functionalities within JDL Jhipster for an Angular project

For instance, I am interested in incorporating functions such as onChange, focusout, onBlur, onClick while passing an extra parameter in jdl like this: <input type="text" class="form-control" name="firstName" (onChange)= ...

What's the deal with TypeScript tsconfig allowing .json imports, but not allowing them in built .js files?

By including the line "resolveJsonModule": true in my project's .tsconfig file, I have successfully implemented direct importing of data from .json files. The project functions properly, even when using nodemon. However, upon building the project and ...

Attempting to retrieve data either by code or with a WHERE condition proves unsuccessful as the data retrieval process yields no results

Seeking assistance with my Angular project that is utilizing a Node.js server and MSSQL express. I am having trouble retrieving data using a WHERE condition in my code. Any help in identifying the missing piece or error would be appreciated. Thank you. // ...

Should one bother utilizing Promise.all within the context of a TypeORM transaction?

Using TypeORM to perform two operations in a single transaction with no specified order. Will utilizing Promise.all result in faster processing, or do the commands wait internally regardless? Is there any discernible difference in efficiency between the t ...

PageObjectModel Playwright, execute the locator().all() function - 'The execution context has been terminated, possibly due to navigating to another...'

Hey there, I'm currently working on a basic test using POM. Here is a snippet from one of my PageObjects Class: import { Expect, Page, Locator } from "@playwright/test"; export class InventoryPage { readonly page: Page; readonly addToC ...

What is the rationale behind Typescript permitting the assignment of an "any" object type to a class object?

Within my codebase, there is a class object that I have initialized: groupNameData: GroupNameData = new GroupNameData(); In addition, I also have an any object called groupNameDatas. groupNameDatas: any; Experiment 1 (class = any) To experiment with ...

RxJS equivalent of a 'finally' callback for Promises

In my component, I have a process that retrieves data from an API. To indicate whether the data is being loaded or not, I use a member variable called loading. Once the data retrieval process is complete, I set loading to false to hide the loading icon. Th ...

Is there a way to include lazy loaded components in the production dist folder using angular-cli bundling?

For my development using angular-cli, I have executed the following commands and codes to construct my project. npm install angular-cli (angular-cli: 1.0.0-beta.10) ng new my-app ng g component lazy-me Then, I included a file named app.router.ts with t ...

The clash between Angular.json's CSS styles configuration and a component's own CSS settings

After experimenting with both angular.json's CSS setting and component CSS setting, I noticed that the component CSS setting is not fully applied. I am looking for a way to ensure that the component CSS setting works properly in a particular componen ...

Compiler raises concerns about potential undefined values in nested objects

In my code snippet, I am experiencing an issue with TypeScript when I try to access an object property after checking for its existence. The sample code can be found here (when strictNullChecks is enabled). 1. let boolVar: number | undefined; 2. 3. if ...

Why has Jasmine decided not to wait for my promises to finish?

Utilizing the FileSystem API to generate a directory with 2 entries for a test, I am facing an issue where Jasmine does not wait for the promise to resolve before executing the test, resulting in failure. Despite using the async wrapper, the problem persis ...

Determining changes in an object with Angular 2 and Ionic 2

Q) How can I detect changes in an object with multiple properties bound to form fields without adding blur events to each individual field? I want to avoid cluttering the page with too many event listeners, especially since it's already heavy. For e ...

Ways to fake an interface using Jest without needing to instantiate it

While Kotlin supports this, I haven't been able to find a way to achieve the same in Jest. My problem arises from having intricate interfaces and arrays of these interfaces where specifying all attribute values is not ideal. Here's an example of ...

Using Angular BehaviorSubject in different routed components always results in null values when accessing with .getValue or .subscribe

I am facing an issue in my Angular application where the JSON object saved in the service is not being retrieved properly. When I navigate to another page, the BehaviorSubject .getValue() always returns empty. I have tried using .subscribe but without succ ...

Is it possible to use uglifyjs to merge multiple files into a single minified file?

I attempted to compress multiple javascript files into one using the uglifyjs tool, but encountered an issue. I ran $node uglifyjs.js to execute the file. Below is the content of the uglify.js file: var fs = require('fs'); var uglifyjs = re ...