Encountering ExpressionChangedAfterItHasBeenCheckedError during ngOnInit when utilizing Promise

I have a simple goal that I am working on:
I want to display data obtained from a service in my component.

This is how it used to work:

In my component:

...

dataSet: String[];

ngOnInit(){
  this._service.getDataId().then(data => this.dataSet = data);
}

...

In my service:

...

getDataId(){
  return this.http.get(adress).toPromise()
    .then(response => response.json() as String[])
    .catch(this.handleError);
} // Return a Promise<String[]>

...

In the view:

... 

<ul>
  <li *ngFor="let data of dataset">
    {{ data['id'] }}
  </li>
</ul>

...

However, I recently made changes to the service function to fetch more data.
Since then, I encountered an ExpressionChangedAfterItHasBeenCheckedError.
Here are the modifications in the service method:

...

getMoreData() {
  const result: Map<string, String[]> = new Map();
  return this.http.get(this.address).toPromise()
    .then((response) => {
      return Promise.all(Array.from(response.json())
      .map(id => {
        return this.http.get(this.address + '/' + id').toPromise()
        .then((rep) => {
          result.set(domain['@href'], rep.json() as String[]);
        }).catch(this.handleError)
      })
    );
  })
  .then(() => result)
  .catch(this.handleError);
} // Return a Promise<Map<string, String[]>>

...

Even though the data is displayed correctly, an error is being raised.
I've tried implementing some workarounds (setTimeout, ChangeDetectorRef) but I'm unsure why it was working previously and not now since both functions return a Promise<Stuff>.

I also understand that I need to initialize my attribute like this in the component to prevent null errors during view loading:

this.moreData: Map<string, String[]> = new Map();

This initialization might also be causing the issue, but what should I do differently?

---
Project specifications
Angular 4.2.2
Typescript 2.3.4

Answer №1

This particular issue is directly connected to the following concern: https://github.com/angular/angular/issues/17725

The main issue lies with the Map<> structure, as it generates a new Iterator each time it is used in the view (for example, when using .entries).

A helpful workaround solution can be found at the provided link.

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

Transform PHP array into a properly structured array or object that can be easily used in JavaScript

My PHP code contains an array that looks like this: $variation = [ attribute_label => "Choose your Color", attribute_name => "pa_choose-your-color", variations => [ "819" => "Red", "820" => "Blue", ...

Tips for using jQuery to identify the most recently modified row in an HTML table

Is there a way to identify the most recently modified (either newly added or changed) row in an HTML table? ...

Stop users from saving the page in Next.js

I'm currently working on a NextJs project that involves building an editor application. I want to ensure that the editor functionality does not work when users attempt to save the page in a different format, similar to how applications like Youtube an ...

Using the split and join methods in PHP for Javascript operations

Can you assist me with writing this code in PHP? I've attempted multiple times but I can't seem to get it right. Any help would be greatly appreciated! return s.split(/\r?\n/).join("<br>"); Thank you in advance! ...

How can I create multiple divs that look alike?

I've taken on the challenge of developing our own interpretation of Conway's "Game of Life" for a project. To represent my 20x20 grid, I decided to create nested divs - the whole grid is one div, each row is a div, and every cell within that is a ...

The class 'GeoJSON' is mistakenly extending the base class 'FeatureGroup'

Encountering an error message in one of my projects: test/node_modules/@types/leaflet/index.d.ts(856,11): error TS2415: Class 'GeoJSON' incorrectly extends base class 'FeatureGroup'. Types of property 'setStyle' are incompa ...

Troubleshooting the need for refreshing in an Angular application when selecting the same menu option repeatedly

Issue with Angular Application: I've noticed a problem in my Angular app where clicking the menu within a component does not trigger a refresh or reload of the component. I want to make sure that the component reloads whenever its specific menu is cl ...

How can I retrieve the number of links pointing to a specific property within a JavaScript object

I am faced with the following dataset: const main = 'test1'; const data = [ { from: 'test1', to: 'test2' }, { from: 'test2', to: 'test3' }, { from: 'test3', to: ...

Creating individual product pages from an array of objects: A step-by-step guide

Is there a way in Next.js to create individual pages for each object in an array with unique URLs? Here is the input array: Input Array const products = [ { url: "item-1", id: 1, name: "Item 1", description: "lor ...

Next.js Server Error: ReferenceError - 'window' is undefined in the application

I am currently in the process of integrating CleverTap into my Next.js application. I have followed the guidelines provided in the documentation Web SDK Quick Start Guide, however, I encountered the following issue: Server Error ReferenceError: window is ...

The process of incorporating injected JavaScript into an existing function

It seems like a classic issue, but I haven't been able to find the solution. I have a form that connects to a MySQL (actually MariaDB) database table called contacts. As part of the jQuery.ready() function, a drop-down list is populated through AJAX ...

How can I use D3.js to form a circular group in an organization structure, link it with a circular image, and connect

Is it possible to create a radial grouped circle using d3.js, similar to the image below: https://i.sstatic.net/1Hwd2.jpg I have written some code as shown below. However, I am facing challenges in connecting every circle with a curved line and displayi ...

Tips for changing the focus between various modals and HTML pages after minimizing a modal with jQuery

I have been utilizing a plugin from to minimize a bootstrap modal within my angular application. The problem I am facing with this plugin is that whenever I minimize a modal, I am unable to interact with the html page, such as typing in input boxes or sc ...

There was an issue encountered when trying to call a PHP file within an HTML table using Ajax and data.html

For a small project, I have various news items that need to be included from the "news_all.php" file into table data within the "dashboard.php" file. Due to the predefined root structure restrictions, using include('news.php') is not an option. I ...

Can an EJS variable be transferred to an Angular ng-repeat filter?

I am currently working on a profile page where I need to display a user's name in plain text using <%= user.local.name %>. This requires querying the database through Mongoose. My issue now is figuring out how to pass that value to an Angular ng ...

Efficient communication across angular modules on a global scale

Currently, I am working on a project that involves Angular2 with multiple modules. One of the main modules is called BaseModule, and in addition to that, there are two extra modules - FirstModule and SecondModule. Each module has its own routing mechanis ...

Automatically focus on the button

Encountering an issue where setting focus on a button upon the input key enter event is not working. While setting focus on inputs functions properly, using the same code on a button does not work at all. Sample HTML Component: <form [formGroup]=&apos ...

Steps to modify the border width upon gaining focus

I am struggling to adjust the border width when my input box is focused. Currently, it has a 1px solid border which changes to a 2px different color solid border upon focus. However, this change in border width is causing the containing div to shift by 1px ...

How can you ensure that an inline <span> automatically moves to the next line if it is too wide?

Struggling with bootstrap badges and trying to arrange <span> elements to appear on the next line when they are too large. On mobile, my page looks like this: Joe Bloggs (Bigger Badge) Joe Bloggs Brother (SMALL BADGE) How can I configure it so t ...

Why am I encountering an issue while trying to access req.user.id?

Having set up passport authentication on my express, node project, I encountered an error when trying to access req.user. The error message displayed is Property 'id' does not exist on type 'User'.ts(2339). Below is the relevant code sn ...